How can I configure Rspec to raise error when stubbing or mocking non-existing methods?

StackOverflow https://stackoverflow.com/questions/21838217

  •  12-10-2022
  •  | 
  •  

문제

Is there a way that I can configure RSpec to raise an error when I am trying to stub/mock a non-existing method.

So, If I have the class

class A
  def foo
  end
end

and I write something like:

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

then RSpec will fail on first line

expect(subject....

by telling me that :foo2 is not message that subject responds to.

도움이 되었습니까?

해결책

This functionality was introduced in the rspec-fire gem and was recently ported into RSpec 3 (currently in beta) as described in this github issue. It is documented in https://relishapp.com/rspec/rspec-mocks/docs/verifying-doubles

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top