rspec - How to test ActiveRecord::RecordNotFound?

前端 未结 2 717
死守一世寂寞
死守一世寂寞 2021-01-28 13:06

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 u         


        
2条回答
  •  耶瑟儿~
    2021-01-28 13:51

    you can try :

    let!(:error_failed) { { error: 'Failed' } }
    
    context 'when people is not found by params' do
      it 'return 404 and render json failed'
        null_object = double.as_null_object
        allow(People).to receive(:find).with(params[:id]).and_raise(ActiveRecord::RecordNotFound.new(null_object)
    
        put :update, format: :json, .....
        expect(response.body).to error_dailed.to_json
        expect(response.status).to .....
      end
    end
    

提交回复
热议问题