Stubbing a call path with Mocha

written December 23 2008 by
Kommen
tagged with
, , ,
No Comments so far. Go, post one!
Previous Let’s Freckle
Credit Card Processing… Next
Let’s Freckle
Credit Card Processing Cheat Sheet
Freckle Beanstalk Integration Launched
Parse natural language date/time strings in Ruby with Chronic

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.

No Comments

Make our day bright and participate!