Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a spec for helper with Ruby on Rails and RSpec

Tags:

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.
like image 351
TK. Avatar asked Jan 03 '10 23:01

TK.


1 Answers

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 
like image 59
plainjimbo Avatar answered Nov 08 '22 01:11

plainjimbo