有没有办法在 rspec 中使用像 'Verify' 这样的匹配器?
Is there a way to use a matcher like 'Verify' in rspec?
到目前为止,我在我的测试框架中使用 'expect',它会在遇到失败条件时停止执行。我想要类似的东西,即使它满足失败条件也应该执行。我可以看到 rspec 中有一个名为 'Verify' 的匹配器,我需要在其中继承 'Test::Unit::TestCase' class,但我的问题是,我的规范文件中需要匹配器这不是写在 ruby class.
下
RSpec 没有开箱即用的方法。
因为 Rspec 旨在测试小型、孤立的逻辑。
失败时,Rspec 匹配器引发错误,因此您可以做的是将匹配器包装在救援块中。
为了满足您的需要,您可以编写这样的包装器:
def report_last(&block)
begin
yield
rescue Exception => e
puts "Failure: #{e}"
end
end
在您的测试用例中:
describe Calculator do
it “should add 2 numbers” do
report_last do
expect(described_class.new(2, 3).sum)to eq(5)
end
end
end
到目前为止,我在我的测试框架中使用 'expect',它会在遇到失败条件时停止执行。我想要类似的东西,即使它满足失败条件也应该执行。我可以看到 rspec 中有一个名为 'Verify' 的匹配器,我需要在其中继承 'Test::Unit::TestCase' class,但我的问题是,我的规范文件中需要匹配器这不是写在 ruby class.
下RSpec 没有开箱即用的方法。 因为 Rspec 旨在测试小型、孤立的逻辑。
失败时,Rspec 匹配器引发错误,因此您可以做的是将匹配器包装在救援块中。 为了满足您的需要,您可以编写这样的包装器:
def report_last(&block)
begin
yield
rescue Exception => e
puts "Failure: #{e}"
end
end
在您的测试用例中:
describe Calculator do
it “should add 2 numbers” do
report_last do
expect(described_class.new(2, 3).sum)to eq(5)
end
end
end