On Sep 17, 2009, at 3:13 PM, Chris Adams wrote:
Hi there,
Forgive the simple sounding question, but I'm struggling to
understand how to spec helper methods in Rails work, and I'm having
no joy, and after spending far, far too long staring at broken code,
I'm hoping someone on the list can shed some light.
All I'm trying to do is spec how a one line helper method for a view
should behave, but I'm not sure what kind of mock object, (if any) I
should be creating if I'm working in Rails.
Here's the method first - I've used it to try to keep a view free of
logic:
module EventsHelper
def filter_check_button_path
params[:filter].blank? ? '/images/buttons/
bt_search_for_events.gif' : '/images/buttons/
bt_refine_this_search.gif'
end
end
And here's my spec code:
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe EventsHelper do
#Delete this example and add some real ones or delete this file
it "should be included in the object returned by #helper" do
included_modules = (class << helper; self;
end).send :included_modules
included_modules.should include(EventsHelper)
end
it "should return the 'refine image search' button if a search has
been run" do
# mock up params hash
params = {}
params[:filter] = true
# create an instanc of the class that should include EventsHelper
by default, as the first test has verified (I think)
@event = Event.new
# call method to check output
@event.filter_check_button_path.should be('/images/buttons/
bt_search_for_events.gif')
end
end
When I've looked through the docs here - http://rspec.info/rails/writing/views.html
, I'm mystified as to where the 'template' object comes from.
I've also tried looking here, which I thought would point me in the
right direction, but alas, no dice. http://jakescruggs.blogspot.com/2007/03/mockingstubbing-partials-and-helper.html
What should I be looking for here in the docs, and what am I doing
wrong?
There should be a "helper" object available in helper specs. Go ahead
an stub it:
params = {:filter => true}
helper.stub!(:params).and_return(params)
@event.filter.....
Scott
_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users