| Class | Spec::Rails::Example::HelperExampleGroup |
| In: |
lib/spec/rails/example/helper_example_group.rb
|
| Parent: | FunctionalExampleGroup |
Helper Specs live in $RAILS_ROOT/spec/helpers/.
Helper Specs use Spec::Rails::Example::HelperExampleGroup, which allows you to include your Helper directly in the context and write specs directly against its methods.
HelperExampleGroup also includes the standard lot of ActionView::Helpers in case your helpers rely on any of those.
class ThingHelper
def number_of_things
Thing.count
end
end
describe "ThingHelper example_group" do
include ThingHelper
it "should tell you the number of things" do
Thing.should_receive(:count).and_return(37)
number_of_things.should == 37
end
end
# File lib/spec/rails/example/helper_example_group.rb, line 42
42: def helper
43: @helper_object ||= returning HelperObject.new do |helper_object|
44: if @helper_being_described.nil?
45: if described_type.class == Module
46: helper_object.extend described_type
47: end
48: else
49: helper_object.extend @helper_being_described
50: end
51: end
52: end
# File lib/spec/rails/example/helper_example_group.rb, line 101
101: def eval_erb(text)
102: helper.instance_eval do
103: ERB.new(text).result(binding)
104: end
105: end
Returns an instance of ActionView::Base with the helper being spec‘d included.
describe PersonHelper do
it "should write a link to person with the name" do
assigns[:person] = mock_model(Person, :full_name => "Full Name", :id => 37, :new_record? => false)
helper.link_to_person.should == %{<a href="/people/37">Full Name</a>}
end
end
module PersonHelper
def link_to_person
link_to person.full_name, url_for(person)
end
end
# File lib/spec/rails/example/helper_example_group.rb, line 73
73: def helper
74: self.class.helper
75: end