rspec:如何在引发错误后测试ensure块(rspec: how to test the ensure block after raised an error)

编程入门 行业动态 更新时间:2024-10-26 04:25:47
rspec:如何在引发错误后测试ensure块(rspec: how to test the ensure block after raised an error)

这是我的begin..rescue..ensure块。 我想编写一些测试用例,在引发错误后,将返回最终结果{} 。 我正在使用rspec 3.3。

def external_call result = ExternalApi.call rescue => e # handle the error, and re-raise Handler.handle(e) raise ensure result.presence || {} end

我已经为救援部分编写了测试用例:

context 'when external api raise error' do it 'handles the error, and re-raise' do allow(ExternalApi).to receive(:call).and_raise(SomeError) expect(Handler).to receive(:handle).with(e) expect { subject.external_call }.to raise_error(SomeError) end end

但是我不确定在重新引发错误之后如何测试确保部分。 这是我的尝试:

it 'returns {} after error raised' do allow(ExternalApi).to receive(:call).and_raise(SomeError) result = subject.external_call expect(result).to eq({}) end

在这种情况下,测试用例将在subject.external_call行中失败,因为它会在那里引发错误。 我不确定如何在重新引发错误后测试这种情况。

Here's my begin..rescue..ensure block. I want to write some test cases that after error is raised, the final result {} will be returned. I am using rspec 3.3.

def external_call result = ExternalApi.call rescue => e # handle the error, and re-raise Handler.handle(e) raise ensure result.presence || {} end

I have wrote test case for the rescue part:

context 'when external api raise error' do it 'handles the error, and re-raise' do allow(ExternalApi).to receive(:call).and_raise(SomeError) expect(Handler).to receive(:handle).with(e) expect { subject.external_call }.to raise_error(SomeError) end end

But I am not sure how to test the ensure part after the error is re-raised. Here's my attempt:

it 'returns {} after error raised' do allow(ExternalApi).to receive(:call).and_raise(SomeError) result = subject.external_call expect(result).to eq({}) end

In this case, the test case will fail in the subject.external_call line, since it will raise error there. I am not sure how to test this cases after the error is re-raised.

最满意答案

当使用带有隐式返回的begin / rescue / ensure块时,ruby将返回要在rescue块中运行的最后一个方法作为返回值,而不是确保。 如果需要返回来自ensure块的值,则必须显式返回该值,或者不将其包括在ensure中,而是移动到begin / rescue块之外。

以下是显示差异的示例。

class TestClass def self.method1 raise 'an error' rescue 'rescue block' ensure 'ensure block' end def self.method2 raise 'an error' rescue 'rescue block' ensure return 'ensure block' end def self.method3 begin raise 'an error' rescue 'rescue block' end 'ensure equivalent block' end end RSpec.describe TestClass do it do # does not work, method1 returns 'rescue block' expect(TestClass.method1).to eql 'ensure block' end it do # does work, as method2 explicitly returns 'ensure block' expect(TestClass.method2).to eql 'ensure block' end it do # does work, as method3 uses 'ensure equivalent block' as the inferred return expect(TestClass.method3).to eql 'ensure equivalent block' end end

When using begin/rescue/ensure block with implicit returns, ruby will return the last method to be run in the rescue block as the return value, not the ensure. If the value from the ensure block needs to be returned, it will either have to be explicitly returned, or not included in an ensure but instead moved outside of the begin/rescue block.

Below is an example which shows the difference.

class TestClass def self.method1 raise 'an error' rescue 'rescue block' ensure 'ensure block' end def self.method2 raise 'an error' rescue 'rescue block' ensure return 'ensure block' end def self.method3 begin raise 'an error' rescue 'rescue block' end 'ensure equivalent block' end end RSpec.describe TestClass do it do # does not work, method1 returns 'rescue block' expect(TestClass.method1).to eql 'ensure block' end it do # does work, as method2 explicitly returns 'ensure block' expect(TestClass.method2).to eql 'ensure block' end it do # does work, as method3 uses 'ensure equivalent block' as the inferred return expect(TestClass.method3).to eql 'ensure equivalent block' end end

更多推荐

本文发布于:2023-04-29 09:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   测试   如何在   rspec   ensure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!