The ELC Community Blog
A knowledge exchange on Ruby on Rails and Agile Development
Testing Libraries
by josh on November 16, 2007
Another way to make Test::Unit act like RSpec
It's really frustrated to try and increase your test coverage in libraries and helpers when you have to instantiate a controller first. So - don't.
To get around this and at the same time make your testing infrastructure easier to manage, you can create a separate directories in your test directory for testing your helpers.
mkdir test/helpers
Create an application_helper_test.rb file in that dir and put the typical stuff in it:
require File.dirname(__FILE__) + '/../test_helper' class ArticlesHelperTest < Test::Unit::TestCase end
Now make sure the Articles Helper is included:
class ArticlesHelperTest < Test::Unit::TestCase include ArticlesHelper end
Now you can test that your helper methods directly:
module ArticlesHelper
def article_title_helper(article)
"#{article.title} - #{article.created_at.strftime("%Y %B %d")}"
end
end
1 def test_article_title_helper
2 assert_equal 'Title - 2007 August 6', article_title_helper(article)
3 end
The same goes for any library. I would argue that if you need to include the library tests in your application, don't put the library in lib. app/lib is ideal for vendor libraries, not local ones.
You'll need to make sure you modify your RCov Task appropriately to include this directory.
Timeline
- Ruby on Rails, OpenSocial Container plugin 0.1.0
- Ruby on Rails primer for Java developers
- OpenSocial container plugin 0.0.1
- Ultraviolet syntax highlighting in Mephisto
- Creating new generator commands
- Testing Libraries
- Rendering views without a web request in rails
- Can I Take a Test Drive?
- Points and Velocity in Trac Reports
- Installing Freeimage + image_science on Leopard
- Writing view helpers with 'yield'
Comments