首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使rspec-mocks“期待receive.with失败”

如何使rspec-mocks“期待receive.with失败”
EN

Stack Overflow用户
提问于 2016-03-23 10:54:45
回答 2查看 1.1K关注 0票数 1

如果使用传递给rspec-模拟的arg匹配器的参数调用目标,则rspec-模拟expect(target).to receive(:message).with(arg_matcher)只会在测试结束时显示错误。是否有一种方法可以强迫它急切地失败--即,当目标被非匹配的参数调用时?RR以这种方式工作。

我面临的问题是,当我使用上面的arg_matcher设置这个模拟时,测试开始失败,因为目标是用不同的params调用的,但是在测试结束之前,另一个断言会失败,所以我只看到了这个断言中的错误,而不是缺失的模拟(这将向我展示预期的params和实际调用的params之间的区别)。

使用rspec-mocks 3.3.2。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-02 02:50:06

我不知道有什么办法能让receive急切地失败。然而,have_received急切地失败了,所以如果它是几个期望中的第一个,那么它将是测试失败和RSpec报告的第一个。

代码语言:javascript
复制
class Foo
  def self.bar(baz)
  end
end

describe "RSpec" do
  it "reports a non-mock expectation failure before a mock expectation failure" do
    expect(Foo).to receive(:bar).with(1)
    expect(true).to be_falsy # RSpec reports this failure
    Foo.bar 2
  end

  it "reports a spy expectation failure when you'd expect it to be reported" do
    allow(Foo).to receive(:bar) # Spy on Foo
    Foo.bar 2
    expect(Foo).to have_received(:bar).with(1) # RSpec reports this failure
    expect(true).to be_falsy
  end

end

详情请参见RSpec间谍的文献

这个解决方案可能是最好的

  • 如果您像我一样更喜欢逻辑安排-行动-断言序列中的测试
  • 如果间谍的期望失败了,你就不关心其他的期望
  • 如果您不介意设置间谍的allow的额外类型,而不介意aggregate_failures

常规模拟和Adarsh的解决方案更好

  • 无论两者是否失败,您都希望看到这两种期望的结果。
  • 如果您不介意aggregate_failures块而不介意设置间谍的allow的额外输入
票数 2
EN

Stack Overflow用户

发布于 2016-06-01 20:53:54

测试开始失败,因为目标是用不同的参数调用的,但是在测试结束之前,另一个断言失败了,所以我只看到了这个断言中的错误,而不是缺少的模拟。

如果我正确地阅读了这篇文章,那么您在一个规范中有多个expect断言,而第一个则失败了,那么您不会看到第二个/第三个断言失败吗?

如果是这样的话,我会考虑使用在3.3节中介绍,它收集故障,但运行后续断言:

代码语言:javascript
复制
aggregate_failures("verifying response") do
  expect(response.status).to eq(200)
  expect(response.headers).to include("Content-Type" => "text/plain")
  expect(response.body).to include("Success")
end

# Which gives you nice errors for all assertions

1) Client returns a successful response
     Got 3 failures:

     1.1) Got 3 failures from failure aggregation block "testing response".
          # ./spec/use_block_form_spec.rb:18
          # ./spec/use_block_form_spec.rb:10

          1.1.1) Failure/Error: expect(response.status).to eq(200)

                   expected: 200
                        got: 404

                   (compared using ==)
                 # ./spec/use_block_form_spec.rb:19

          1.1.2) Failure/Error: expect(response.headers).to include("Content-Type" => "application/json")
                   expected {"Content-Type" => "text/plain"} to include {"Content-Type" => "application/json"}
                   Diff:
                   @@ -1,2 +1,2 @@
                   -[{"Content-Type"=>"application/json"}]
                   +"Content-Type" => "text/plain",
                 # ./spec/use_block_form_spec.rb:20

          1.1.3) Failure/Error: expect(response.body).to eq('{"message":"Success"}')

                   expected: "{\"message\":\"Success\"}"
                        got: "Not Found"

                   (compared using ==)
                 # ./spec/use_block_form_spec.rb:21

     1.2) Failure/Error: expect(false).to be(true), "after hook failure"
            after hook failure
          # ./spec/use_block_form_spec.rb:6
          # ./spec/use_block_form_spec.rb:10

     1.3) Failure/Error: expect(false).to be(true), "around hook failure"
            around hook failure
          # ./spec/use_block_form_spec.rb:12

您还可以将描述块标记为aggregate_failures

代码语言:javascript
复制
RSpec.describe ClassName, :aggregate_failures do
  # stuff
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36176200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档