如何在Rails 5中期望RSpec中的Params哈希?
我正在升级到Rails 5,即使我传递了我应该的数据,它已经破坏了我的RSpec。
问题显然在这里:
expected: ({"name"=>"MyString"}) got: ("MyString"} permitted: true>)
这意味着我需要能够修复我的控制器断言,以便它能够预期后者。 这是需要改变的路线。
expect_any_instance_of(Hospital).to receive(:update).with({ "name" => "MyString" })
可能是这样的
expect_any_instance_of(Hospital).to receive(:update).with(params: { "name" => "MyString" }, permitted: true)
我只是不知道语法是什么,并且无法在Rails 5的分散文档中找到它,或者在Rails 5上有关RSpec的不存在的注释/ Stack Overflow问题。
完整错误和控制器规格
2) HospitalsController PUT update with valid params updates the requested hospital Failure/Error: if @hospital.update(hospital_params) # received :update with unexpected arguments expected: ({"name"=>"MyString"}) got: ("MyString"} permitted: true>) Diff: @@ -1,2 +1,2 @@ -[{"name"=>"MyString"}] +["MyString"} permitted: true>] # ./app/controllers/hospitals_controller.rb:54:in `block in update'
控制器规格方法
describe "PUT update" do describe "with valid params" do it "updates the requested hospital" do hospital = Hospital.create! valid_attributes # Assuming there are no other hospitals in the database, this # specifies that the Hospital created on the previous line # receives the :update_attributes message with whatever params are # submitted in the request. expect_any_instance_of(Hospital).to receive(:update).with({ "name" => "MyString" }) put :update, {:id => hospital.to_param, :hospital => { "name" => "MyString" }}, valid_session end it "assigns the requested hospital as @hospital" do hospital = Hospital.create! valid_attributes put :update, {:id => hospital.to_param, :hospital => valid_attributes}, valid_session expect(assigns(:hospital)).to eq(hospital) end it "redirects to the hospital" do hospital = Hospital.create! valid_attributes put :update, {:id => hospital.to_param, :hospital => valid_attributes}, valid_session expect(response).to redirect_to(hospital) end end ...etc
您是否尝试过使用ActionController :: Parameters对象作为您期望的值?
如:
expect_any_instance_of(Hospital).to receive(:update).with(ActionController::Parameters.new('name':'MyString'))