duck_punching

Ruby, Rails and tech. No ducks were harmed in the making of this blog.

Archive for the 'actionview' Category

Using ActionView in standalone tests

When writing Rails plugins I like to be able to test them as a stand-alone library rather than inside a specific Rails project. I find it more productive and it avoids clashes with other plugins. It also allows you to test it against multiple rails versions.

One frustration I have had is when just want to use ActionView in your tests, because you might be writing a view plugin, if you require ‘action_view’ you get the following error:

MissingSourceFile: no such file to load -- html/document

This is because the path to the html-scanner library has not been added to the loadpath. This only happens in ActionController. But if we don’t need or want the overhead of ActionController the following line will include html-scanner in the loadpath and all will be well.

$:.unshift Gem.searcher.find('actionpack').full_gem_path 
  + '/lib/action_controller/vendor/html-scanner'

This finds the actionpack gem to get the full path. Then with the path to the html-scanner folder, adds it to the loadpath ($:). Now no more pesky load errors.

If there is a cleaner way, let me know.

2 comments