[mocha-developer] Mocking objects to test Rails Controllers

Rick Moynihan rick at calicojack.co.uk
Wed May 28 11:12:34 EDT 2008

Hi all,

I'm currently considering the use of Mocha to aid in writing my Rails 
unit/functional tests.

I currently have a Rails model which mixes in a series of methods to 
allow it to talk to an external service.  The code is roughly of this form:

class Person < ActiveRecord::Base

    include SMSNotifier

    def send_notification(message)
       # more code/interactions...

Assuming I want to test that a call to send_notification calls send_sms 
on the SMSNotifier module, what is the best way to use Mocha to do this? 
    Obviously I don't want my tests calls to send_notification to 
actually do any SMS sending :-)

I'm assuming Mocha can monkey-patch it's own method/expectations over 
the SMSNotifier module.  In Java, I'd use dependency injection, but I'm 
trying to find the Ruby way here.

Also I've seen that Rails has a folder called test/mocks, can anyone 
provide any examples or links as to the use of this in Rails/Mocha?

Thanks in advance for your assistance,


More information about the mocha-developer mailing list