dchelimsky / rspec-rails

RSpec extension library for Ruby on Rails

This URL has Read+Write access

rspec-rails / lib / spec / rails / example / functional_example_group.rb
100644 107 lines (97 sloc) 3.412 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
require 'action_controller/test_case'
 
module Spec
  module Rails
    module Example
      class FunctionalExampleGroup < ActionController::TestCase
        def setup
          # no-op to override AC::TC's setup w/ conflicts with the before(:each) below
        end
 
        attr_reader :request, :response
 
        # The params hash accessed within a view or helper. Use this before
        # rendering a view or calling a helper to provide data used by the
        # view or helper.
        #
        # == Examples
        # # in a view spec
        # params[:name] = "David"
        # render
        # response.should have_tag("div.name","David")
        #
        # # in a helper spec
        # params[:first_name] = "David"
        # params[:last_name] = "Chelimsky"
        # helper.full_name.should == "David Chelimsky"
        def params
          request.parameters
        end
 
        # Provides access to the flash hash. Use this after rendering a
        # view, calling a helper or calling a controller action.
        #
        # == Examples
        # post :create
        # flash[:notice].should == "Success!"
        def flash
          @controller.__send__ :flash
        end
 
        # Provides acces to the session hash. Use this before or after
        # rendering a view, calling a helper or calling a controller action.
        def session
          request.session
        end
        
        # Overrides the <tt>cookies()</tt> method in
        # ActionController::TestResponseBehaviour, returning a proxy that
        # accesses the requests cookies when setting a cookie and the
        # responses cookies when reading one. This allows you to set and read
        # cookies in examples using the same API with which you set and read
        # them in controllers.
        #
        # == Examples (Rails 2.0 > 2.2)
        #
        # cookies[:user_id] = {:value => '1234', :expires => 1.minute.ago}
        # get :index
        # response.should be_redirect
        #
        # == Examples (Rails 2.3)
        #
        # Rails 2.3 changes the way cookies are made available to functional
        # tests (and therefore rspec controller specs), only making single
        # values available with no access to other aspects of the cookie. This
        # is backwards-incompatible, so you have to change your examples to
        # look like this:
        #
        # cookies[:foo] = 'bar'
        # get :index
        # cookies[:foo].should == 'bar'
        def cookies
          @cookies ||= Spec::Rails::Example::CookiesProxy.new(self)
        end
        
        alias_method :orig_assigns, :assigns
 
        # :call-seq:
        # assigns()
        #
        # Hash of instance variables to values that are made available to
        # views. == Examples
        #
        # #in thing_controller.rb
        # def new
        # @thing = Thing.new
        # end
        #
        # #in thing_controller_spec
        # get 'new'
        # assigns[:registration].should == Thing.new
        #--
        # NOTE - Even though docs only use assigns[:key] format, this supports
        # assigns(:key) for backwards compatibility.
        #++
        def assigns(key = nil)
          if key.nil?
            _assigns_hash_proxy
          else
            _assigns_hash_proxy[key]
          end
        end
 
      end
    end
  end
end