2016-07-24 75 views
6

我正在升級到Rails 5,這已經打破了我的RSpec,即使我傳遞了我應該的數據。如何期待Rails 5中RSpec中的Params散列?

顯然,問題在這裏:

expected: ({"name"=>"MyString"}) 
got: (<ActionController::Parameters {"name"=>"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,或不存在票據/ on Rails的有關RSpec的堆棧溢出問題5.

完整的錯誤和控制器規格

2) HospitalsController PUT update with valid params updates the requested hospital 
Failure/Error: if @hospital.update(hospital_params) 

    #<Hospital id: 43, name: "MyString", reference_code: "RefCod", image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, contact_phone: "+61-000-000-000", website_link: "www.example.com", street_number: "01", street: "Somewhere St", suburb: "Suburb", state: "ACT", postcode: "1111", description: "MyText MyText MyText MyText MyText MyText MyText M...", areas_of_specialization: "MyText MyText MyText MyText MyText MyText MyText M...", created_at: "2016-07-24 22:28:24", updated_at: "2016-07-24 22:28:24"> received :update with unexpected arguments 
    expected: ({"name"=>"MyString"}) 
      got: (<ActionController::Parameters {"name"=>"MyString"} permitted: true>) 
    Diff: 
    @@ -1,2 +1,2 @@ 
    -[{"name"=>"MyString"}] 
    +[<ActionController::Parameters {"name"=>"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 

回答

6

您是否嘗試過使用ActionController :: Parameters對象作爲您期望的值?

如:

expect_any_instance_of(Hospital).to receive(:update).with(ActionController::Parameters.new('name':'MyString'))

+4

泰非常多,不知道你可以只調用了這樣的一些參數。這裏是什麼工作,請注意在最後包含許可 - 'expect_any_instance_of(Hospital).to接收(:update).with(ActionController :: Parameters.new('name':'MyString')。permit(:name))' – user2792268

相關問題