Updated Rails to 2.1.0
[gitorious:georgyos-clone.git] / vendor / plugins / rspec_on_rails / lib / spec / rails / example / helper_example_group.rb
1 module Spec
2   module Rails
3     module Example
4       # Helper Specs live in $RAILS_ROOT/spec/helpers/.
5       #
6       # Helper Specs use Spec::Rails::Example::HelperExampleGroup, which allows you to
7       # include your Helper directly in the context and write specs directly
8       # against its methods.
9       #
10       # HelperExampleGroup also includes the standard lot of ActionView::Helpers in case your
11       # helpers rely on any of those.
12       #
13       # == Example
14       #
15       #   class ThingHelper
16       #     def number_of_things
17       #       Thing.count
18       #     end
19       #   end
20       #
21       #   describe "ThingHelper example_group" do
22       #     include ThingHelper
23       #     it "should tell you the number of things" do
24       #       Thing.should_receive(:count).and_return(37)
25       #       number_of_things.should == 37
26       #     end
27       #   end
28       class HelperExampleGroup < FunctionalExampleGroup
29         class HelperObject < ActionView::Base
30           def protect_against_forgery?
31             false
32           end
33         end
34         
35         class << self
36           # The helper name....
37           def helper_name(name=nil)
38             @helper_being_described = "#{name}_helper".camelize.constantize
39             send :include, @helper_being_described
40           end
41           
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
53         end
54         
55         # Returns an instance of ActionView::Base with the helper being spec'd
56         # included.
57         #
58         # == Example
59         #
60         #   describe PersonHelper do
61         #     it "should write a link to person with the name" do
62         #       assigns[:person] = mock_model(Person, :full_name => "Full Name", :id => 37, :new_record? => false)
63         #       helper.link_to_person.should == %{<a href="/people/37">Full Name</a>}
64         #     end
65         #   end
66         #
67         #   module PersonHelper
68         #     def link_to_person
69         #       link_to person.full_name, url_for(person)
70         #     end
71         #   end
72         #
73         def helper
74           self.class.helper
75         end
76
77         # Reverse the load order so that custom helpers which are defined last
78         # are also loaded last.
79         ActionView::Base.included_modules.reverse.each do |mod|
80           include mod if mod.parents.include?(ActionView::Helpers)
81         end
82
83         before(:all) do
84           @controller_class_name = 'Spec::Rails::Example::HelperBehaviourController'
85         end
86
87         before(:each) do
88           @controller.request = @request
89           @controller.url = ActionController::UrlRewriter.new @request, {} # url_for
90
91           @flash = ActionController::Flash::FlashHash.new
92           session['flash'] = @flash
93
94           ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
95         end
96
97         def flash
98           @flash
99         end
100
101         def eval_erb(text)
102           helper.instance_eval do
103             ERB.new(text).result(binding)
104           end
105         end
106
107         # TODO: BT - Helper Examples should proxy method_missing to a Rails View instance.
108         # When that is done, remove this method
109         def protect_against_forgery?
110           false
111         end
112
113         Spec::Example::ExampleGroupFactory.register(:helper, self)
114
115         protected
116         def _assigns_hash_proxy
117           @_assigns_hash_proxy ||= AssignsHashProxy.new helper
118         end
119
120       end
121
122       class HelperBehaviourController < ApplicationController #:nodoc:
123         attr_accessor :request, :url
124
125         # Re-raise errors
126         def rescue_action(e); raise e; end
127       end
128     end
129   end
130 end