Spec:
before do
Logger.should_receive( :write ).with 'Log message 1'
end
it 'works' do
get '/'
end
Sinatra App:
get '/'
Logger.write( 'Log message 1' )
Logger.write( 'Log message 2' )
end
This spec fails because of 'Log message 2'. How to tell RSpec to ignore any other messages, and only test for the expected message?
You need to stub the method that will be receiving the message before the message expectation.
The
stubmethod is deprecated in RSpec 3, instead use one of the followingA method stub is an instruction to an object (real or test double) to return a known value in response to a message.
In this case, tell the
Loggerobject to return the valuenilwhen it receives thewritemessage (the first time).So your
beforeblock should look like this