I'm writing my first Rails plugin, and I'm quite stuck testing it. In simplified form:
module Flashbulb
def flash_messages()
return <<END
<!-- some HTML -->
<script type='text/javascript'> #{update_flash_messages()} </script>
END
end
def update_flash_messages(args = { :page => nil })
update_flash = lambda { |target|
# do stuff
}
if args[:page] != nil
update_flash.call(args[:page])
else
update_page do |new_page|
update_flash.call(new_page)
end
end
end
end
This works just fine; I have the following code in init.rb that extends every view:
# Include hook code here
require 'flashbulb'
ActionView::Base.send :include, Flashbulb
Then I can call flash_messages
in my views:
%body
= flash_messages
... and update_flash_messages
in my RJS:
update_flash_messages :page => page
The thing I can't figure out, though, is how to test flash_messages
in my plugin; it seems I'm not setting something up properly before invoking update_page
.
The following test code:
require 'test_helper'
require 'action_view'
require 'action_controller'
require 'action_view/test_case'
require File.dirname(__FILE__) + '/../lib/flashbulb.rb'
include Flashbulb
class FlashbulbTest < ActionView::TestCase
test "flash_messages returns correct HTML" do
html = flash_messages()
assert_dom_equal('<!-- some HTML --><!-- some JS -->', html)
end
end
... just gives me the following error (trace trimmed for brevity):
1) Error:
test_flash_messages_returns_correct_HTML(FlashbulbTest):
NoMethodError: undefined method `with_output_buffer' for nil:NilClass
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/prototype_helper.rb:577:in `initialize'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/prototype_helper.rb:1029:in `new'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.4/lib/action_view/helpers/prototype_helper.rb:1029:in `update_page'
./../lib/flashbulb.rb:29:in `update_flash_messages'
I suspect I'm missing something pretty simple here ... as I said this is my first Rails plugin and I'm pretty much flying blind.
I'm also concerned that perhaps I'm misusing update_page
inside update_flash_messages
- it works when inside a real Rails app but I'm not sure how idiomatic my usage is. Maybe this is the issue, rather than a problem with the test itself?