Testing your Rails Plugins April 24th, 2008

A great feature in Ruby is the integrated testing. Ruby on Rails also implements and extends the Ruby testing framework, and if you’re not testing your Ruby code you should start doing it now!

Normally, when I write tests for a class, I mock out everything not related directly to the class (I do this with mocha). For example, when writing tests for the Mollom gem, I mocked out the whole XMLRPC API, because I can’t rely on them to give me all test cases.

# from http://github.com/DefV/ruby-mollom/tree/master/test/mollom_test.rb
def test_server_list
  xml_rpc = mock
  xml_rpc.expects(:call).
          with('mollom.getServerList', is_a(Hash)).
          returns(['http://172.16.0.1', 'http://172.16.0.2', 'https://172.16.0.2'])

  XMLRPC::Client.stubs(:new).with('xmlrpc.mollom.com', '/1.0').returns(xml_rpc)

  assert_equal [
          {:ip => '172.16.0.1', :proto => 'http'}, 
          {:ip => '172.16.0.2', :proto => 'http'}, 
          {:ip => '172.16.0.2', :proto => 'https'}
         ], @mollom.server_list
end

This way, I’m only testing the behaviour of that specific class, so if something fails somewhere, I can pinpoint the problem, while being sure it’s not a problem in the Mollom API.

While writing a test for some view plugin the other day, I was doing the same thing. I stubbed out everything from ActionView/ActionController which I used (content_tag and url_for and such). Suddenly I realised that wasn’t a good idea.

Rails plugins tests should use the Rails Framework, because the plugins themselves rely on it deeply. To be sure the plugin keeps working while the framework changes, I just have to run my tests, which is exactly what I want.

So I do it this way now:

# from http://github.com/DefV/title_helper/tree/master/test/title_helper_test.rb
require 'test/unit'
require File.dirname(__FILE__) + '/../../../../config/boot.rb'
require File.dirname(__FILE__) + '/../../../../config/environment.rb'

class TitleHelperTest < Test::Unit::TestCase
  def setup
    @helper = ActionView::Base.new
  end

  # Replace this with your real tests.
  def test_title_method_with_no_title_set
    assert_equal "foobar", @helper.title(:site_name => 'foobar')
  end
  ...
end

How do you test your Rails Plugins?

tags: , , , l

9 Responses to “Testing your Rails Plugins”

  • 3 months ago Over 50 Life Insurance Guide said

    Hello and good evening to you friend, I hope that all is well?! Thank you for sharing this valuable information. Keep up the great work.

  • 3 months ago Phd Dissertation said

    Hello, you have made some excellent points here, many thanks for sharing this great content! I hope that you can manage to keep this great work flowing. All the best.

  • 2 months ago colocation said

    This is a truly awesome write-up about the Ruby and its integrated testing! After reading this, we really get to understand the importance of testing the Ruby Code. I was really excited to learn so much about how to go about testing our Rails Plugins and I am sure that many like me would have found this to be one of the most useful and informative blogs that they have come across in a while!!

  • 2 months ago optimizare site web said

    This plugin is good for SEO too. I’ve used this tool for my SEO company website and it works very well. I am glad to see that this thread is promoted in the online media too.

  • 2 months ago oferta rca ieftin said

    I am not very sure that this plugin will work with the latest version of ruby. Is it safe for this kind of job.

  • 12 days ago poker slots advisor said

    I really enjoyed for this website and that to helpful info in this website. This website is providing the different articles in this website. I am very much satisfied by this info that to helpful info in this blog.

  • 3 days ago moulin a vendre said

    This is a good post. This post gives truly quality information. I¡¯m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good work Minoterie pour farine

  • about 23 hours ago bilverkstad växjö said

    Thanks for this interesting post. Its a interesting thing that needs to be shared. Ive been trying with the rails plugins for quite some time.

  • about 20 hours ago bilverkstad vaxjo said

    This is a good plugin. Thanks for sharing. I hope you are gonna post some more good info. thx.

Leave a Reply