测试规格助手:如何期望在挂钩之后发生故障 我有一个规格助手,在后钩中,检查了方法调用的输出。钩子本身只是从随附的模块调用了一种方法,我已经成功地测试了所有方法。 现在我想...

问题描述 投票:0回答:1
around

,我可以检查

example.exception
是否设置为预期值,但是我在文档中没有找到任何“重置”的任何地方,因此可以将测试标记为成功

RSpec.describe(Example, type: :interactor) do around do |example| example.run expect(example.exception).to be_a(ValidateOutput::Error) end it "is included with metadata" do output end end

试图期望发生错误只是为同一测试造成第二个错误:

  RSpec.describe(Example, type: :interactor) do
    around do |example|
      expect do
        example.run
      end.to raise_error(ValidateOutput::Error)
    end

    it "is included with metadata" do
      output
    end
  end
我怎么能告诉RSPEC,这应该通过,因为我期望错误?
    

在搜索并完成对本网站的询问进行正式查询的工作后,我找到了解决方案。

使用专用方法并分别测试此方法是第一步(因此您知道该方法按预期起作用)。

如果您想确保它被称为,那么只需期望该方法被调用
self

RSpec.describe(Example, type: :interactor) do it "executes validate_output! with matching metadata" do expect(self).to receive(:validate_output!) output end end
ruby rspec
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.