0

私はRubyとRailsに比較的慣れておらず、頭を悩ませることができない問題に悩まされています。以下に示すように設定された2つのモデルPricingRuleTypeとPricingRuleがあります。

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

createメソッドとupdateメソッドの機能テストを以下に示します。

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つと同様の方法でセットアップされた他のモデルがありますが、この関係はなく、エラーは発生しないようです。ご協力いただければ幸いです。

4

1 に答える 1

0

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

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

そして、テストでは「product_ruleを更新する必要があります」:product_rule => @updateセットアップでは、次のように定義する必要があります。

:pricing_rule_type_ids => pricing_rule_types(:revoo_pricing_rule).id
于 2012-04-16T08:39:28.507 に答える