我想爲我的控制器之一編寫一些rspec聯合測試,並且我正在運行int有關stubbing REST api調用的一點疑惑。rails rspec存根並提出響應代碼404響應
所以我有這個REST調用哪個接收水果ID並返回一個特定的水果信息,我想測試何時REST給我回應代碼404(未找到)。理想情況下,我會踩滅的方法調用和返回錯誤代碼
在控制器
def show
@fruit = FruitsService::Client.get_fruit(params[:id])
end
規格/控制器/ 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
這給這個
Failure/Error: get :show, {id: 'wrong_id'}
RuntimeError:
RuntimeError
您的測試出您磕碰 – bjhaid