On a recent Radiant customization project, I needed to write some tests for a front-end extension. One situation called for a stub object, so I built out the pathing and created the stub object as I’m used to doing in Rails. In extensions/extension_name/test/mocks/test I created a some_mailer.rb object to mock a mailer model, overriding the mailer’s deliver_mail method:

require File.dirname(__FILE__) + '/../../../app/models/some_mailer'

class SomeMailer
  def self.deliver_mail(mail)
    @mail_posted = true
  end

  def self.mail_posted
    @mail_posted
  end

  def self.reset_posted
    @mail_posted = false
  end
end

The other two methods are used in my functional test to detect if the deliver_mail method has been called.

Unfortunately, Radiant doesn’t handle the load path like traditional Rails. In Rails, the classes in test/mocks/test are loaded before anything else, allowing one to stub out any file they like. Radiant doesn’t load these stubs/mocks at all.

It’s fairly simple to resolve this however. Simply add some requires in your extensions/extension\_name/test/test_helper.rb file:

Dir[File.dirname(__FILE__) + "/mocks/test/*.rb"].each do |file|
 require file
end

Another note. If you have any unit tests relating to the model that’s been stubbed, you’ll need to explicitly require the real model. Something like:

require File.dirname(__FILE__) + '/../../app/models/some_mailer'

Thanks to Sean Cribbs for helping me square this away.

Jul 25, 2007 · Subscribe · Archive · Projects · Twitter · GitHub · Flickr