2012-04-16 3 views
0

RubyとRailsには比較的新しいですが、問題を抱えています。私は以下のように設定された2つのモデルPricingRuleTypeとPricingRuleを持っています。has_and_belongs_to_manyの問題でのRailsの機能テスト

class PricingRuleType < ActiveRecord::Base 
    attr_accessible :type_of_rule 
    has_and_belongs_to_many :product_rules 
    validates :type_of_rule, :presence => true 
end 

class ProductRule < ActiveRecord::Base 
    has_and_belongs_to_many :pricing_rule_types 
    validates :pricing_rule_type, :discount_per_unit, :number_of_units, :presence => true 
    validates :discount_per_unit, :numericality => {:greater_than_or_equal_to => 0.01} 
end 

のための機能テストの作成と更新方法は以下の通りである:

1) Error: 
test_should_create_product_rule(ProductRulesControllerTest): 
NoMethodError: undefined method `stringify_keys' for "revoo_rule":String 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_assignment.rb:69:in `assign_attributes' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/base.rb:495:in `initialize' 
    /Users/kushaldsouza/Documents/Client Projects/Revoo/app/controllers/product_rules_controller.rb:44:in `new' 
    /Users/kushaldsouza/Documents/Client Projects/Revoo/app/controllers/product_rules_controller.rb:44:in `create' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/base.rb:167:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/rendering.rb:10:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/callbacks.rb:18:in `block in process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:414:in `_run__580034481047679801__process_action__1008436725406406543__callbacks' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in `__run_callback' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/callbacks.rb:17:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/rescue.rb:29:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/notifications.rb:123:in `block in instrument' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/notifications.rb:123:in `instrument' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/instrumentation.rb:29:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/params_wrapper.rb:205:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/base.rb:121:in `process' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/rendering.rb:45:in `process' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:464:in `process' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:49:in `process' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:385:in `post' 
    /Users/kushaldsouza/Documents/Client Projects/Revoo/test/functional/product_rules_controller_test.rb:27:in `block (2 levels) in <class:ProductRulesControllerTest>' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/testing/assertions.rb:55:in `assert_difference' 
    /Users/kushaldsouza/Documents/Client Projects/Revoo/test/functional/product_rules_controller_test.rb:26:in `block in <class:ProductRulesControllerTest>' 



2) Error: 
test_should_update_product_rule(ProductRulesControllerTest): 
NoMethodError: undefined method `each' for "1":String 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:308:in `replace' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/associations/collection_association.rb:41:in `writer' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/associations/builder/association.rb:51:in `block in define_writers' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_assignment.rb:78:in `each' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/attribute_assignment.rb:78:in `assign_attributes' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/persistence.rb:212:in `block in update_attributes' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/transactions.rb:208:in `transaction' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/transactions.rb:293:in `with_transaction_returning_status' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/persistence.rb:211:in `update_attributes' 
    /Users/kushaldsouza/Documents/Client Projects/Revoo/app/controllers/product_rules_controller.rb:63:in `block in update' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/mime_responds.rb:269:in `call' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/mime_responds.rb:269:in `retrieve_response_from_mimes' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/mime_responds.rb:194:in `respond_to' 
    /Users/kushaldsouza/Documents/Client Projects/Revoo/app/controllers/product_rules_controller.rb:62:in `update' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/base.rb:167:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/rendering.rb:10:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/callbacks.rb:18:in `block in process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:414:in `_run__580034481047679801__process_action__1983642789249364420__callbacks' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in `__run_callback' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/callbacks.rb:17:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/rescue.rb:29:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/instrumentation.rb:30:in `block in process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/notifications.rb:123:in `block in instrument' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.2.1/lib/active_support/notifications.rb:123:in `instrument' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/instrumentation.rb:29:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/params_wrapper.rb:205:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.2.1/lib/active_record/railties/controller_runtime.rb:18:in `process_action' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/base.rb:121:in `process' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/abstract_controller/rendering.rb:45:in `process' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/metal/testing.rb:17:in `process_with_new_base_test' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:464:in `process' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:49:in `process' 
    /Users/kushaldsouza/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.2.1/lib/action_controller/test_case.rb:390:in `put' 
    /Users/kushaldsouza/Documents/Client Projects/Revoo/test/functional/product_rules_controller_test.rb:44:in `block in <class:ProductRulesControllerTest>' 

私のテストコントローラ

は以下の通りである:

require 'test_helper' 

class ProductRulesControllerTest < ActionController::TestCase 
    fixtures :product_rules 
    setup do 
    @product_rule = product_rules(:one) 
    @update = { 
     :number_of_units => 3, 
     :discount_per_unit => 2.3, 
     :pricing_rule_type => pricing_rule_types(:revoo_pricing_rule) 
    } 
    end 

    test "should get index" do 
    get :index 
    assert_response :success 
    assert_not_nil assigns(:product_rules) 
    end 

    test "should get new" do 
    get :new 
    assert_response :success 
    end 

    test "should create product_rule" do 
    assert_difference('ProductRule.count') do 
     post :create, :product_rule => :revoo_rule 
    end 

    assert_redirected_to product_rule_path(assigns(:product_rule)) 
    end 

    test "should show product_rule" do 
    get :show, id: @product_rule 
    assert_response :success 
    end 

    test "should get edit" do 
    get :edit, id: @product_rule 
    assert_response :success 
    end 

    test "should update product_rule" do 
    put :update, id: @product_rule.to_param, :product_rule => @update 
    assert_redirected_to product_rule_path(assigns(:product_rule)) 
    end 

    test "should destroy product_rule" do 
    assert_difference('ProductRule.count', -1) do 
     delete :destroy, id: @product_rule 
    end 

    assert_redirected_to product_rules_path 
    end 
end 

私は、なぜ私は午前完全にわかりませんそのエラーを取得し、しばらくそれに固執しています。これは何らかの理由で私がhas_and_belongs_to_many関係を含んでいたためです。私は上記の2つの方法と同様の方法でセットアップされている他のモデルを持っていますが、この関係はなく、エラーを投げているようには見えません。

答えて

0

テストで "product_ruleを作成する必要があります"とは何ですか:revoo_rule? それは次のようになります。

post :create, :product_rule => @product_rule.to_param 

とテストであなた持っている "product_rule更新する必要があります":

:pricing_rule_type_ids => pricing_rule_types(:revoo_pricing_rule).id 
+0

:それはのように定義されなければならない設定でproduct_rule => @Update をrevoo_ruleは私のルールです私のfitureで定義されています。私はこれを試して、それが動作するかどうかを知らせます。ありがとう – kushaldsouza

+0

ありがとうございます。私は実際にidsフィールドを間違って設定していた – kushaldsouza

関連する問題