I am trying to write some rspec unite test for one of my controller and I am running int a little confusion about stubbing a REST api call.
so I have this REST call which take the fruit id and return a particular fruit information and I want to test when REST give me respond code 404 (Not Found). Ideally, I would stub out the method call and return the error code
In Controller
def show
@fruit = FruitsService::Client.get_fruit(params[:id])
end
spec/controller/fruits_controller_spec.rb
describe '#show' do
before do
context 'when a wrong id is given' do
FruitsService::Client.any_instance
.stub(:get_fruit).with('wrong_id')
.and_raise <----------- I think this is my problem
get :show, {id: 'wrong_id'}
end
it 'receives 404 error code' do
expect(response.code).to eq('404')
end
end
This giving this
Failure/Error: get :show, {id: 'wrong_id'}
RuntimeError:
RuntimeError