Testing 澄清此默认rspec控制器测试并使其通过
这是使用我创建的脚手架生成的默认测试Testing 澄清此默认rspec控制器测试并使其通过,testing,rspec,ruby-on-rails-3.1,Testing,Rspec,Ruby On Rails 3.1,这是使用我创建的脚手架生成的默认测试 describe "PUT update", focus: true do describe "with valid params" do it "updates the requested purchase_order" do current_valid_attr = valid_attributes purchase_order = PurchaseOrder.create! current_val
describe "PUT update", focus: true do
describe "with valid params" do
it "updates the requested purchase_order" do
current_valid_attr = valid_attributes
purchase_order = PurchaseOrder.create! current_valid_attr
# Assuming there are no other purchase_orders in the database, this
# specifies that the PurchaseOrder created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
# PurchaseOrder.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
# put :update, :id => purchase_order.id, :purchase_order => {'these' => 'params'}
PurchaseOrder.any_instance.should_receive(:update_attributes).with(current_valid_attr)
put :update, :id => purchase_order.id, :purchase_order => current_valid_attr
end
问题是我不明白它应该做什么,我不能用正确的属性让它通过。下面是我运行测试时的错误
Failures:
1) PurchaseOrdersController PUT update with valid params updates the requested purchase_order
Failure/Error: put :update, :id => purchase_order.id, :purchase_order => current_valid_attr
#<PurchaseOrder:0x007fe3027521e0> received :update_attributes with unexpected arguments
expected: ({"id"=>nil, "supplier_id"=>1, "no"=>1305, "no_rujukan"=>"Guiseppe Abshire", "jenis"=>"P", "mula_on"=>Sat, 23 Aug 2003 14:11:42 MYT +08:00, "created_at"=>nil, "updated_at"=>nil})
got: ({"id"=>nil, "supplier_id"=>"1", "no"=>"1305", "no_rujukan"=>"Guiseppe Abshire", "jenis"=>"P", "mula_on"=>"2003-08-23 14:11:42 +0800", "created_at"=>nil, "updated_at"=>nil})
工厂
FactoryGirl.define do
factory :purchase_order do
association :supplier
sequence(:no) { |n| Random.rand(1000...9999) }
sequence(:no_rujukan) { |n| Faker::Name.name }
sequence(:jenis) { |n| PurchaseOrder::PEROLEHAN[Random.rand(0..3).to_i] }
sequence(:mula_on) { |n| Random.rand(10.year).ago }
end
end
此测试检查当请求到达
PurchaseOrdersController#update
操作update_属性
时,是否调用了其中一个PurchaseOrder
模型的方法,并将请求参数正确传递给此方法
这里的问题(如错误消息所述)是使用所有类型为String
的属性哈希调用update\u attributes
。这是因为update
操作中最可能使用的params
hash(包含所有请求参数)的所有值都是字符串
另一方面,current\u valid\u attr
散列包含不同类型的值,如Fixnum
和Time
。当需要比较预期值和接收值时,您会得到一个错误,因为,例如,no
属性预期为Fixnum1305
,但在提交请求后,它被转换为string,并update\u attributes
received string'1305'
解决此问题的方法之一是确保current\u valid\u attr
中的所有值都是字符串:
no = 1305
mula_on = Time.now
# explicitly convert all attributes to String
current_valid_attr = { :no => no.to_s, :mula_on => mula_on.to_s }
已更新
有一种方法可以在测试中用于将参数的散列转换为控制器在
params
散列中接收到的相同形式。您能显示有效属性的定义吗?@KL-7:我已经更新了问题,我正在使用FactoryGirl生成有效属性,这是否意味着我需要逐个显式转换属性?您可以尝试使用方法。它在内部用于将传递给get
、post
和其他请求方法的所有参数的值转换为字符串。尝试以下操作:参数化值(Factory.build(:purchase\u order.attributes)
。
no = 1305
mula_on = Time.now
# explicitly convert all attributes to String
current_valid_attr = { :no => no.to_s, :mula_on => mula_on.to_s }