rspec - How to test ActiveRecord::RecordNotFound?

允我心安 提交于 2020-04-17 22:06:34

问题


I have a method to update people's attribute, and it will rescue ActiveRecord::RecordNotFound if the people cannot be found. The method is:

  def update
    @people= People.find(params[:id])
    if @people.update(people_params)
      render json: { success: 'Success' }
    else
      render :edit
    end
  rescue ActiveRecord::RecordNotFound => e
    render json: { error: 'Failed') }
  end

And I want to test the situation when the record not found, here's my test for now:

    let(:people) { create(:people) }
    let(:people_id) { people.id }
    let(:user) { people}
    # Other tests...
    context 'when person not found' do
      let(:exception) { ActiveRecord::RecordNotFound }

      # What should I write so that I can let the record not been found?

      before { allow(People).to receive(:find).and_raise(exception) }

      it 'responds with json containing the error message' do
        expect(JSON.parse(response.body)).to eq({error:'Error'})
      end
    end

I want my test executed under the condition that records not found. But I don't know how to do it. I tried to set let(people) {nil} but it not work. Is there an anyway to do that? Thanks!


回答1:


This is not a good solution to begin with. In Rails you want to use rescue_from to handle common errors on the controller level.

class ApplicationController
  rescue_from ActiveRecord::RecordNotFound, with: :not_found

  def not_found
    respond_to do |format|
      format.json { head :404 }
    end
  end
end

This lets you use inheritance to DRY your code.

render json: { error: 'Failed') }

Is a huge anti-pattern. If the request failed you should tell the client by sending the correct HTTP status code. Don't reinvent the wheel. Especially not when your solution is a square wheel. If your JS relies on monkeying around with a json response to see if the request was a success or not you're doing it wrong.

If you want to test that your controller handles a missing resource correctly you would do:

let(:people) { create(:people) }
let(:people_id) { people.id }
let(:user) { people}

it "returns the correct response code if the person cannot be found" do
  get '/people/notarealid'
  expect(response).to have_http_status :not_found
end

This does not use any stubbing and actually tests the implementation.



来源:https://stackoverflow.com/questions/61132089/rspec-how-to-test-activerecordrecordnotfound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!