mardi 30 août 2016

Unit Testing Controller to Check That Render Is Called

All I want to do is test that the controller is actually calling render.

Note:

I don't care about the output in this test, so I don't want to check the response.body. I just want to make sure the method of render is being sent to the controller.

controller action:

  def create
    render json: { error: "some error" }, status: :unprocessable_entity
  end

spec:

This is literally the only expectation the spec so there is nothing conflicting.

it "renders error in json" do
  args = {
    json:   { error: "some error" },
    status: :unprocessable_entity
  }

  expect(controller).to receive(:render).with(args)
  post :create, user: { upload: upload }
end

# rspec spec/controllers
# FAILURE:
expected: ({:json=>{:error=>"some error"}, :status=>:unprocessable_entity})
got: (no args)

Here is possible clue of me doing something wrong. This is what happens when I remove the argument expectation:

it "renders error in json" do
  expect(controller).to receive(:render) # no args expectation
  post :create, price_patch_upload: { upload: upload } # called two times??
end

# rspec spec/controllers
# FAILURE:
expected: 1 time with any arguments
received: 2 times with any arguments

What am I missing here?

Aucun commentaire:

Enregistrer un commentaire