I setup a mock object and told it to expect a check for nil and to return false:
status = double('status')
status.should_receive(:nil?).and_return(false)
I only expect the call to nil? to occur once, but I got an error in my rspec test, saying that status received nil? twice.
Is there a way to get rspec to show where/how each call occurred?
adding the ‘–backtrace’ option did not work.
Try something like this:
This tells rspec to allow two invocations and call the associated block each time. The
callermethod generates a full backtrace which you should be able to analyze onstdout. We also returnfalseto stay on the code-path we’re testing.If the two backtraces are hard to distinguish and you’re only interested in the second (unexpected) invocation, then set up two successive expectations:
Here the double will return false on the first invocation and call the block on the second.
Reference for setting responses on expectations:
https://github.com/rspec/rspec-mocks#setting-responses