public
Rubygem
Description: Makes tests easy on the fingers and the eyes
Homepage: http://www.thoughtbot.com/projects/shoulda
Clone URL: git://github.com/thoughtbot/shoulda.git
100644 198 lines (178 sloc) 7.523 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
module ThoughtBot # :nodoc:
  module Shoulda # :nodoc:
    module Controller # :nodoc:
      module HTML # :nodoc: all
        def self.included(other)
          other.class_eval do
            extend ThoughtBot::Shoulda::Controller::HTML::ClassMethods
          end
        end
  
        module ClassMethods
          def make_show_html_tests(res)
            context "on GET to :show" do
              setup do
                record = get_existing_record(res)
                parent_params = make_parent_params(res, record)
                get :show, parent_params.merge({ res.identifier => record.to_param })
              end
 
              if res.denied.actions.include?(:show)
                should_not_assign_to res.object
                should_redirect_to res.denied.redirect
                should_set_the_flash_to res.denied.flash
              else
                should_assign_to res.object
                should_respond_with :success
                should_render_template :show
                should_not_set_the_flash
              end
            end
          end
 
          def make_edit_html_tests(res)
            context "on GET to :edit" do
              setup do
                @record = get_existing_record(res)
                parent_params = make_parent_params(res, @record)
                get :edit, parent_params.merge({ res.identifier => @record.to_param })
              end
        
              if res.denied.actions.include?(:edit)
                should_not_assign_to res.object
                should_redirect_to res.denied.redirect
                should_set_the_flash_to res.denied.flash
              else
                should_assign_to res.object
                should_respond_with :success
                should_render_template :edit
                should_not_set_the_flash
                should_render_a_form
                should "set @#{res.object} to requested instance" do
                  assert_equal @record, assigns(res.object)
                end
              end
            end
          end
 
          def make_index_html_tests(res)
            context "on GET to :index" do
              setup do
                record = get_existing_record(res) rescue nil
                parent_params = make_parent_params(res, record)
                get(:index, parent_params)
              end
 
              if res.denied.actions.include?(:index)
                should_not_assign_to res.object.to_s.pluralize
                should_redirect_to res.denied.redirect
                should_set_the_flash_to res.denied.flash
              else
                should_respond_with :success
                should_assign_to res.object.to_s.pluralize
                should_render_template :index
                should_not_set_the_flash
              end
            end
          end
 
          def make_new_html_tests(res)
            context "on GET to :new" do
              setup do
                record = get_existing_record(res) rescue nil
                parent_params = make_parent_params(res, record)
                get(:new, parent_params)
              end
 
              if res.denied.actions.include?(:new)
                should_not_assign_to res.object
                should_redirect_to res.denied.redirect
                should_set_the_flash_to res.denied.flash
              else
                should_respond_with :success
                should_assign_to res.object
                should_not_set_the_flash
                should_render_template :new
                should_render_a_form
              end
            end
          end
 
          def make_destroy_html_tests(res)
            context "on DELETE to :destroy" do
              setup do
                @record = get_existing_record(res)
                parent_params = make_parent_params(res, @record)
                delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
              end
        
              if res.denied.actions.include?(:destroy)
                should_redirect_to res.denied.redirect
                should_set_the_flash_to res.denied.flash
          
                should "not destroy record" do
                  assert_nothing_raised { assert @record.reload }
                end
              else
                should_set_the_flash_to res.destroy.flash
                if res.destroy.redirect.is_a? Symbol
                  should_respond_with res.destroy.redirect
                else
                  should_redirect_to res.destroy.redirect
                end
 
                should "destroy record" do
                  assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
                    @record.reload
                  end
                end
              end
            end
          end
 
          def make_create_html_tests(res)
            context "on POST to :create with #{res.create.params.inspect}" do
              setup do
                record = get_existing_record(res) rescue nil
                parent_params = make_parent_params(res, record)
                @count = res.klass.count
                post :create, parent_params.merge(res.object => res.create.params)
              end
        
              if res.denied.actions.include?(:create)
                should_redirect_to res.denied.redirect
                should_set_the_flash_to res.denied.flash
                should_not_assign_to res.object
          
                should "not create new record" do
                  assert_equal @count, res.klass.count
                end
              else
                should_assign_to res.object
                should_set_the_flash_to res.create.flash
                if res.create.redirect.is_a? Symbol
                  should_respond_with res.create.redirect
                else
                  should_redirect_to res.create.redirect
                end
 
                should "not have errors on @#{res.object}" do
                  assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
                end
              end
            end
          end
 
          def make_update_html_tests(res)
            context "on PUT to :update with #{res.create.params.inspect}" do
              setup do
                @record = get_existing_record(res)
                parent_params = make_parent_params(res, @record)
                put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
              end
 
              if res.denied.actions.include?(:update)
                should_not_assign_to res.object
                should_redirect_to res.denied.redirect
                should_set_the_flash_to res.denied.flash
              else
                should_assign_to res.object
                should_set_the_flash_to(res.update.flash)
                if res.update.redirect.is_a? Symbol
                  should_respond_with res.update.redirect
                else
                  should_redirect_to res.update.redirect
                end
                
                should "not have errors on @#{res.object}" do
                  assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
                end
              end
            end
          end
        end
      end
    end
  end
end