btakita / rr

RR (Double Ruby) is a test double framework that features a rich selection of double techniques and a terse syntax.
http://github.com/rr/rr
MIT License
501 stars 58 forks source link

Equivalent of new_instance_of #81

Closed GerryG closed 12 years ago

GerryG commented 12 years ago

I can't seem to figure out how this is supposed to be done. I've tried many things, and I can't get it to record the calls (spy), on new instances of my Mailer class (from ActionMailer::Base).

This first part seems to work, although I'm not clear on the documentation that indicates any_instance_of doesn't work the same for mocks as stubs. It seems that declaring the expectation, I should get the method calls recorded.

 any_instance_of(Mailer) do
    mock(Mailer).account_info.with_any_args
 end

This part always fails. I've been able to make it work with 1.8.7 and patching up rr-1.0.0 which still has that method:

  Mailer.should have_received.account_info(@juser, "Password Reset",
      "You have been given a new temporary password.  " +
      "Please update your password once you've signed in. ")

I've tried to use instance_of as well with very similar results.

What am I missing?

mcmire commented 12 years ago

You may have figured this out by now but

  1. You don't need to use any_instance_of here. You're saying that when a new Mailer is created, you want to set an expectation that Mailer.account_info is called. Instead, you need to make that expectation outside of the any_instance_of. (Unless account_info is actually an instance method.)
  2. You're using mock() in conjunction with spies. This isn't necessary; instead, either mock() the method before the test (which will record the call and then auto-check it after the test), or stub() it (which will also record the call) and then check the expectation after the test manually with the spy.
GerryG commented 12 years ago

What is confusing here relates to how Mailer works. The class gets called, and it creates a new object and calls the same method on the new object. In your own mailer class (Mailer for me), you declare these methods as instance methods.

On the other hand, my example doesn't seem exactly right. There should be an object passed to the block and that is what I wanted mocked.

Now I'm trying to figure out whether it matters if I spy on the class or the created instances.

Thanks anyway.