PlugTest is my solution to what was a shockingly difficult time testing my very first gem/plugin: link_block. After rifling through problem after problem trying to use ActionController::TestCase when testing views helpers, I realized that the plugin and helpers worked in the Rails app, so why not just test in there.
Problem is that you can’t really release a plugin without tests, and having tests be dependent on your Rails app is silly.
So here it is: version 1.0 of PlugTest.
I tried to make it as lightweight as possible. I removed the javascript and images, and told Initializer not to load ActiveRecord, ActiveResource, or ActionMailer. The bulk of the code is in PlugTestController and config/initializers/plug_test.rb
$ gem install ryana-plug_test
add the following line to your [PLUGIN_DIR]/test/test_helper.rb (or whatever)
require 'plug_test'
and call the pt_setup method from your TestUnit setup like so:
class MySweetPluginTest < Test::Unit::TestCase def setup pt_setup end end
Bing bang boom.
Whenever you want to test a helper just call:
pt_helper { [CODE YOU WANT TO TEST] }
pt_helper will return the output of the call and setup your @response so you can do:
res = pt_helper { link_to 'my link text', :action => 'test' } assert_response :success assert_select 'a', 'my link text' assert(/my link text/.match(res))
It’s that easy. First crack. It only solves a small need of mine, so fork and pull request away.
Recommend me on Working With Rails or give me a shout out on Twitter @angilly if you think this is awesome.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.