Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expect a Params hash in RSpec in Rails 5?

I'm upgrading to Rails 5, which has broken my RSpec even though I'm passing the data I should be.

The problem is obviously here:

expected: ({"name"=>"MyString"})
got: (<ActionController::Parameters {"name"=>"MyString"} permitted: true>)

Which means I need to be able to fix my controller assertion so that it expects the latter. This is the line that needs changing.

expect_any_instance_of(Hospital).to receive(:update).with({ "name" => "MyString" })

Probably to something like this

expect_any_instance_of(Hospital).to receive(:update).with(params: { "name" => "MyString" }, permitted: true)

I just don't know what the syntax is, and can't find it anywhere in the scattered documentation for Rails 5, or non existent notes/Stack Overflow questions concerning RSpec on Rails 5.

Full error and controller spec

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'

Controller spec method

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
like image 340
Jeremy E Avatar asked Jul 24 '16 22:07

Jeremy E


1 Answers

Have you tried just using a ActionController::Parameters object as the value you're expecting?

As in:

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

like image 93
josh Avatar answered Oct 21 '22 18:10

josh