Stubbing a call path with Mocha

written December 23 2008 by
{ Kommen }
Comments 0

Stubbing named_scope calls can be a real nightmare. If you use Rails’ named scopes as often as I do, you probably know this.
Imagine this code in a controller:

1 user = @account.users.activated.find_by_email

Using off-the-shelf Mocha you would stub it with something like the following:

1 @account.stubs('users').returns(mock('users', :activated => mock('activated', :find_by_email => users(:kommen))))

This doesn’t fit my coding style though. So lets fix this.

With this little monkey patch extracted from freckle you can now do this:

1 @account.stub_path("users.activated.find_by_email").returns(users(:kommen))

Now we can talk.

You've reached the end of this page. Feel free to dig into the archives of this blog or subscribe to our newsfeed.