To add a collaborator to this project you will need to use the Relish gem to add the collaborator via a terminal command. Soon you'll be able to also add collaborators here!
More about adding a collaboratorSpy on a stubbed method on a pure mock
You can use have_received
to verify that a stubbed method was invoked,
rather than setting an expectation for it to be invoked beforehand.
- Scenarios
-
- verify a stubbed method
- verify a stubbed method with message expectations
- fail to verify a stubbed method
- fail to verify message expectations
- generate a spy message
- verify a stubbed method
-
- Given
-
a file named "verified_spy_spec.rb" with:
describe "have_received" do it "passes when the expectation is met" do invitation = double('invitation', :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
- verify a stubbed method with message expectations
-
- Given
-
a file named "verified_message_expectations_spec.rb" with:
describe "have_received" do it "passes when the expectation is met" do invitation = double('invitation', :deliver => true) 2.times { invitation.deliver(:expected, :arguments) } invitation.should have_received(:deliver). with(:expected, :arguments). twice end end
- When
-
I run
rspec verified_message_expectations_spec.rb
- Then
- the examples should all pass
- fail to verify a stubbed method
-
- Given
-
a file named "failed_spy_spec.rb" with:
describe "have_received" do it "fails when the expectation is not met" do invitation = double('invitation', :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"
- fail to verify message expectations
-
- Given
-
a file named "failed_message_expectations_spec.rb" with:
describe "have_received" do it "fails when the arguments are different" do invitation = double('invitation', :deliver => true) invitation.deliver(:unexpected) invitation.should have_received(:deliver).with(:expected, :arguments) end end
- When
-
I run
rspec failed_message_expectations_spec.rb
- Then
- the output should contain "expected: (:expected, :arguments)"
- And
- the output should contain "got: (:unexpected)"
- generate a spy message
-
- Given
-
a file named "spy_message_spec.rb" with:
describe "have_received" do subject(:invitation) { double('invitation', :deliver => true) } before { invitation.deliver } it { should have_received(:deliver) } end
- When
-
I run
rspec --format documentation spy_message_spec.rb
- Then
- the output should contain "should have received deliver"
Last published almost 7 years ago by myronmarston.