Writing a spec for helper with Ruby on Rails and RSpec

TK. picture TK. · Jan 4, 2010 · Viewed 15.2k times · Source

I have been writing specs for controllers and models, but I have never written a helper spec. I have no idea where I start.

I have the following snippet in application_helper.rb

  def title(page_title)
    content_for(:title) { page_title }
  end
  • How should I write a helper spec on the code?
  • Also if there's any open-source Rails app to show good helper testing/specing, do let me know.

Answer

plainjimbo picture plainjimbo · Oct 11, 2012

From the rspec-rails docs on Helper Specs:

Helper specs live in spec/helpers, and mix in ActionView::TestCase::Behavior.

Provides a helper object which mixes in the helper module being spec'd, along with ApplicationHelper (if present).

require 'spec_helper'
describe ApplicationHelper do
  describe "#title" do
    it "displays the title" do
      # helper is an instance of ActionView::Base configured with the
      # ApplicationHelper and all of Rails' built-in helpers
      expect(helper.title).to match /Some Title/
    end
  end 
end