#115 new
Chad Humphries

[#13616] [CODE included] Testing RJS helper methods

Reported by Chad Humphries | November 19th, 2007 @ 06:25 AM

Hi,

I like to test my RJS helper methods using RSpec. These are the helper methods that have a page object implicitly provided for their use.

As an example, for:

update_page {|page| page.notify("kitten")} in the controller

With the helper being:

def notify(message)
  page[:notice].replace_html message
end

I can do:

describe AjaxHelper, "notify" do
  it "should update notice area" do
    rjs_for.notify("kitten").should have_rjs(:chained_replace_html, "notice")
  end
end

To do this, I include a Helper module, but it could be in core using code similar to the following (all in

dsl/behaviour/helper.rb).

In ExampleMethods module, alongside eval_erb:

def rjs_for
  HelperRJSPageProxy.new(self)
end

Then where other classes are defined (e.g. HelperEvalContextController):

class HelperRJSPageProxy
  def initialize(context)
    @context = context
  end

  def method_missing(method, *arguments)
    block = Proc.new { |page|  @lines = []; page.send(method, *arguments) }
    @context.response.body = ActionView::Helpers::PrototypeHelper::JavaScriptGenerator.new(@context, &block).to_s
    @context.response
  end
end

I checked out the RSpec trunk, thinking to make a patch, but failed to get far with understanding RSpec's own spec layout and speccing the spec.

Hope you find the code useful! It's been handy to be able to test RJS helpers, and it would be great to have in core.

Comments and changes to this ticket

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Behaviour Driven Development for Ruby.

Shared Ticket Bins

People watching this ticket