rspec - 如何测试 ActiveRecord::RecordNotFound? [英] rspec - How to test ActiveRecord::RecordNotFound?

查看:25
本文介绍了rspec - 如何测试 ActiveRecord::RecordNotFound?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法来更新人的属性,如果找不到人,它会拯救ActiveRecord::RecordNotFound.方法是:

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

我希望我的测试在没有找到记录的情况下执行.但我不知道该怎么做.我试图设置 let(people) {nil} 但它不起作用.有没有办法做到这一点?谢谢!

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!

推荐答案

这不是一个好的解决方案.在 Rails 中,您希望使用 rescue_from 来处理控制器级别的常见错误.

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

这让您可以使用继承来 DRY 代码.

This lets you use inheritance to DRY your code.

render json: { error: 'Failed') }

是一个巨大的反模式.如果请求失败,您应该通过发送正确的 HTTP 状态代码来告诉客户端.不要重新发明轮子.尤其是当您的解决方案是方轮时更是如此.如果您的 JS 依赖于 json 响应来查看请求是否成功,那么您就做错了.

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.

这篇关于rspec - 如何测试 ActiveRecord::RecordNotFound?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆