RSpec / RSpec Mocks / Docs / Spies
This page comes from RSpec Mocks 2.99.4. It is not part of the 3.12.4 docs. See the latest RSpec Mocks docs.
Spy on a stubbed method on a partial mock
You can also use have_received to verify that a stubbed method was invoked on a partial mock.
Scenario: verify a stubbed method
- Given a file named "
verified_spy_spec.rb" with:rubydescribe "have_received" do it "passes when the expectation is met" do invitation = Object.new invitation.stub(:deliver => true) invitation.deliver invitation.should have_received(:deliver) end end - When I run `rspec verified_spy_spec.rb`
- Then the examples should all pass
Scenario: fail to verify a stubbed method
- Given a file named "
failed_spy_spec.rb" with:rubydescribe "have_received" do it "fails when the expectation is not met" do invitation = Object.new invitation.stub(:deliver => true) invitation.should have_received(:deliver) end end - When I run `rspec failed_spy_spec.rb`
- Then the output should contain "
expected: 1 time" - And the output should contain "
received: 0 times"
Source: features/spies/spy_partial_mock_method.feature at v2.99.4, RSpec Mocks (MIT license).