最终在 RSpec 中抛出异常的测试方法
Testing methods that eventually throw an exception in RSpec
我有一个方法如下:
if response.fetch('ok')
response.fetch(response_key) { [] }
elsif response.fetch('error') == 'token_revoked'
ErrorReporter.increment('access_revoked', source: { account_id: account_id }, sporadic: true)
fail(RemoteService::AccessRevoked, 'Our access to your account was revoked, please re-authorize.')
else
ErrorReporter.increment(
'bad_request',
source: {
account_id: account_id
error: response.fetch('error')
},
sporadic: true
)
fail(RemoteService::InvalidRequest, 'Something went wrong communicating with the remote service, please try again')
end
我正在尝试测试请求 returns 出现 token_revoked
错误时的场景。我想确保测试指定在这种情况下,我们将错误报告给我们的 ErrorReporting 服务。
因此,我的规范如下所示:
it 'records the failure with our error reporting service' do
expect(ErrorReporter).to receive(:increment).with(
'bad_request',
source: {
account_id: 1
},
sporadic: true
)
available_channels.fetch
end
然而,这个规范总是失败,因为在调用 ErrorReporter 之后,代码立即调用 fail
这使得我的规范失败。有谁知道我如何在处理我知道代码现在将要抛出的不可避免的异常时验证对我的错误报告器的调用?
您可以expect errors在代码中提出。为了让异常被RSpec捕获,你需要使用如下的块:
it 'records the failure with our error reporting service' do
expect(ErrorReporter).to receive(:increment).with(
'bad_request',
source: {
account_id: 1
},
sporadic: true
)
expect { available_channels.fetch }
.to raise_error RemoteService::InvalidRequest
end
异常导致您的示例失败,因为它没有被挽救。为防止错误,您可以将方法调用包装在 begin ... rescue
块中:
it 'records the failure with our error reporting service' do
expect(ErrorReporter).to receive(:increment).with(
'bad_request',
source: {
account_id: 1
},
sporadic: true
)
begin
available_channels.fetch
rescue => RemoteService::InvalidRequest
# would cause the example to fail
end
end
在另一个示例中,您还应该 expect
要引发的错误。
我有一个方法如下:
if response.fetch('ok')
response.fetch(response_key) { [] }
elsif response.fetch('error') == 'token_revoked'
ErrorReporter.increment('access_revoked', source: { account_id: account_id }, sporadic: true)
fail(RemoteService::AccessRevoked, 'Our access to your account was revoked, please re-authorize.')
else
ErrorReporter.increment(
'bad_request',
source: {
account_id: account_id
error: response.fetch('error')
},
sporadic: true
)
fail(RemoteService::InvalidRequest, 'Something went wrong communicating with the remote service, please try again')
end
我正在尝试测试请求 returns 出现 token_revoked
错误时的场景。我想确保测试指定在这种情况下,我们将错误报告给我们的 ErrorReporting 服务。
因此,我的规范如下所示:
it 'records the failure with our error reporting service' do
expect(ErrorReporter).to receive(:increment).with(
'bad_request',
source: {
account_id: 1
},
sporadic: true
)
available_channels.fetch
end
然而,这个规范总是失败,因为在调用 ErrorReporter 之后,代码立即调用 fail
这使得我的规范失败。有谁知道我如何在处理我知道代码现在将要抛出的不可避免的异常时验证对我的错误报告器的调用?
您可以expect errors在代码中提出。为了让异常被RSpec捕获,你需要使用如下的块:
it 'records the failure with our error reporting service' do
expect(ErrorReporter).to receive(:increment).with(
'bad_request',
source: {
account_id: 1
},
sporadic: true
)
expect { available_channels.fetch }
.to raise_error RemoteService::InvalidRequest
end
异常导致您的示例失败,因为它没有被挽救。为防止错误,您可以将方法调用包装在 begin ... rescue
块中:
it 'records the failure with our error reporting service' do
expect(ErrorReporter).to receive(:increment).with(
'bad_request',
source: {
account_id: 1
},
sporadic: true
)
begin
available_channels.fetch
rescue => RemoteService::InvalidRequest
# would cause the example to fail
end
end
在另一个示例中,您还应该 expect
要引发的错误。