2

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.

1 Answer 1

4

Try something like this:

status.should_receive(:nil?).twice { puts caller; false }

This tells rspec to allow two invocations and call the associated block each time. Thecaller method generates a full backtrace which you should be able to analyze onstdout. We also returnfalse to 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:

status.should_receive(:nil?).and_return(false)
status.should_receive(:nil?) { puts caller; false }

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.