【发布时间】:2014-01-09 19:04:57
【问题描述】:
我正在尝试为我的一个控制器编写一些 rspec 联合测试,但我对存根 REST api 调用有点困惑。
所以我有这个 REST 调用,它获取水果 id 并返回特定的水果信息,我想测试 REST 何时给我响应代码 404(未找到)。理想情况下,我会将方法调用存根并返回错误代码
在控制器中
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
这个给这个
Failure/Error: get :show, {id: 'wrong_id'}
RuntimeError:
RuntimeError
【问题讨论】:
-
您的测试超出了您正在存根的上下文
标签: ruby-on-rails ruby unit-testing rspec