Skip to content

Add test spies

gitlab-qa-bot requested to merge github/fork/thoughtbot/jf-jq-spies into master

Created by: jferris

  • Adds have_received matcher
  • Extends Proxy/MethodDouble/MessageExpectation
  • Records all invocations on stubbed methods
  • Resolves #220 (closed).

I ended up including a have_received method in rspec-mocks and not touching rspec-expectations, because:

  • have_received returns a good result object (a matcher) that you can use for boolean checks and a failure message, which we want in rspec-mocks
  • have_received would make little sense in rspec-expectations if you weren't also using rspec-mocks

I resurrected some invocation recording functionality in received_messages? that seems to have been somewhat broken for a while. It was added in 2007 for ZenTest support, and seems to have been moved forward with every release without modification since then. Previously it only ever recorded messages for null object mocks; I updated it to record messages for any stubbed method.

The matcher works in negative cases using should_not/not_to; I decided not to implement never, because combining should_not and never has proven to be confusing to bourne users. I figured it would be easier to just have one, standard way to write negative expectations.

There was one weird situation I wasn't sure how to deal with on MethodDouble. Every MethodExpectation expects to have a expected_from backtrace line, because previously errors would be inserted where the expectation was set. This isn't necessary with have_received, because it raises a normal exception from where the expectation failed. I ended up passing a useless backtrace line with caller(1)[0] that isn't ever used.

Feedback welcome.

Merge request reports