【问题标题】:How can I configure Rspec to raise error when stubbing or mocking non-existing methods?如何配置 Rspec 在存根或模拟不存在的方法时引发错误?
【发布时间】:2014-02-17 19:59:09
【问题描述】:

当我尝试存根/模拟不存在的方法时,有没有一种方法可以配置 RSpec 以引发错误。

所以,如果我有课

class A
  def foo
  end
end

我写了类似的东西:

describe A do
  describe '#foo' do
    it 'foos' do
      expect(subject).to receive(foo2)
      # fire
      subject.foo
    end
  end
end

那么 RSpec 将在第一行失败

expect(subject....

告诉我:foo2 不是subject 响应的消息。

【问题讨论】:

  • Iirc 有一个 gem,问题是在模拟之后可能会添加一个方法。
  • raise unless subject.respond_to?(:foo2)

标签: ruby rspec rspec-rails


【解决方案1】:

此功能是在rspec-fire gem 中引入的,最近被移植到 RSpec 3(目前处于测试阶段)中,如this github issue 中所述。它记录在https://relishapp.com/rspec/rspec-mocks/docs/verifying-doubles

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-10
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2015-08-30
    • 2010-11-03
    相关资源
    最近更新 更多