0

我对 Ruby 和 rails 比较陌生,并且被困在一个我无法理解的问题上。我有两个模型 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 关系这一事实。我有其他模型以与上述两个类似的方式设置,但没有这种关系,它们似乎没有抛出任何错误。非常感谢任何帮助。

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 回答