【发布时间】:2016-08-07 16:39:53
【问题描述】:
我需要解决我的应用程序的测试。 Coverage 正在抱怨一行代码评估与 MongoDB (rescue Mongo::Error::NoServerAvailable => _e) 的连接并呈现错误。
你认为我应该用什么来测试这个:
def index
render json: Complex.all.to_json
rescue Mongo::Error::NoServerAvailable => _e
render json: { error_description: 'no database available' }, status: 503
end
我正在尝试使用以下内容进行测试:
it 'should return an exception' do
get :index
expect(response).to raise_exception
end
我发现我应该使用
.and_raise(IOError)
但我不确定在哪里使用它才能使它落在线上。 实际上,如果我停止 Mongo,我可以让它落在异常上,但这不是我的想法。
感谢您的宝贵时间。
【问题讨论】:
-
@DaveSchweisguth 到目前为止运气不佳。我知道我必须用 expect(response).to raise_exception 来测试它听起来不错,但我还没有找到使动作落在救援错误上的方法。救援 Mongo::Error::NoServerAvailable => _e
标签: ruby-on-rails ruby mongodb rspec rspec-rails