resource_controller 0.5.3 → 0.6.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +18 -14
- data/VERSION.yml +5 -0
- data/generators/scaffold_resource/templates/rspec/routing_spec.rb +8 -8
- data/generators/scaffold_resource/templates/rspec/views/index_spec.rb +2 -2
- data/lib/resource_controller.rb +5 -14
- data/lib/resource_controller/base.rb +2 -2
- data/lib/resource_controller/class_methods.rb +2 -2
- data/lib/resource_controller/controller.rb +3 -2
- data/lib/resource_controller/helpers.rb +0 -6
- data/lib/resource_controller/helpers/nested.rb +1 -1
- data/lib/resource_controller/singleton.rb +2 -2
- data/test/app/controllers/{application.rb → application_controller.rb} +0 -2
- data/test/app/controllers/cms/personnel_controller.rb +2 -0
- data/test/app/controllers/cms/photos_controller.rb +6 -0
- data/test/app/controllers/photos_controller.rb +1 -0
- data/test/app/models/personnel.rb +3 -0
- data/test/app/models/photo.rb +1 -0
- data/test/app/views/cms/photos/edit.rhtml +17 -0
- data/test/app/views/cms/photos/index.rhtml +20 -0
- data/test/app/views/cms/photos/new.rhtml +16 -0
- data/test/app/views/cms/photos/show.rhtml +8 -0
- data/test/config/boot.rb +6 -5
- data/test/config/database.yml +6 -10
- data/test/config/environment.rb +4 -4
- data/test/config/environments/development.rb +0 -4
- data/test/config/initializers/inflections.rb +14 -0
- data/test/config/routes.rb +3 -0
- data/test/db/migrate/013_create_personnel.rb +11 -0
- data/test/db/migrate/014_add_personnel_id_to_photos.rb +9 -0
- data/test/db/schema.rb +9 -3
- data/test/test/fixtures/personnel.yml +5 -0
- data/test/test/functional/cms/options_controller_test.rb +1 -1
- data/test/test/functional/cms/photos_controller_test.rb +43 -0
- data/test/test/functional/cms/products_controller_test.rb +1 -1
- data/test/test/functional/comments_controller_test.rb +2 -9
- data/test/test/functional/images_controller_test.rb +1 -8
- data/test/test/functional/people_controller_test.rb +2 -9
- data/test/test/functional/photos_controller_test.rb +2 -9
- data/test/test/functional/posts_controller_test.rb +2 -9
- data/test/test/functional/projects_controller_test.rb +12 -9
- data/test/test/functional/somethings_controller_test.rb +2 -9
- data/test/test/functional/tags_controller_test.rb +2 -9
- data/test/test/functional/users_controller_test.rb +2 -9
- data/test/test/test_helper.rb +4 -3
- data/test/test/unit/accessors_test.rb +2 -2
- data/test/test/unit/account_test.rb +1 -1
- data/test/test/unit/action_options_test.rb +1 -1
- data/test/test/unit/base_test.rb +1 -1
- data/test/test/unit/comment_test.rb +1 -1
- data/test/test/unit/failable_action_options_test.rb +1 -1
- data/test/test/unit/helpers_test.rb +1 -1
- data/test/test/unit/image_test.rb +1 -1
- data/test/test/unit/option_test.rb +1 -1
- data/test/test/unit/photo_test.rb +1 -1
- data/test/test/unit/post_test.rb +1 -1
- data/test/test/unit/project_test.rb +1 -1
- data/test/test/unit/response_collector_test.rb +1 -1
- data/test/test/unit/something_test.rb +1 -1
- data/test/test/unit/tag_test.rb +1 -1
- data/test/test/unit/urligence_test.rb +1 -1
- metadata +162 -122
- data/Rakefile +0 -35
- data/init.rb +0 -1
- data/lib/resource_controller/version.rb +0 -9
- data/test/log/development.log +0 -3350
- data/test/log/test.log +0 -174947
- data/test/log/thin.log +0 -12
- data/test/vendor/plugins/shoulda/Rakefile +0 -32
- data/test/vendor/plugins/shoulda/bin/convert_to_should_syntax +0 -40
- data/test/vendor/plugins/shoulda/init.rb +0 -3
- data/test/vendor/plugins/shoulda/lib/shoulda.rb +0 -43
- data/test/vendor/plugins/shoulda/lib/shoulda/active_record_helpers.rb +0 -580
- data/test/vendor/plugins/shoulda/lib/shoulda/color.rb +0 -77
- data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/controller_tests.rb +0 -467
- data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/html.rb +0 -201
- data/test/vendor/plugins/shoulda/lib/shoulda/controller_tests/formats/xml.rb +0 -170
- data/test/vendor/plugins/shoulda/lib/shoulda/gem/proc_extensions.rb +0 -14
- data/test/vendor/plugins/shoulda/lib/shoulda/gem/shoulda.rb +0 -239
- data/test/vendor/plugins/shoulda/lib/shoulda/general.rb +0 -118
- data/test/vendor/plugins/shoulda/lib/shoulda/private_helpers.rb +0 -22
@@ -1,201 +0,0 @@
|
|
1
|
-
module ThoughtBot # :nodoc:
|
2
|
-
module Shoulda # :nodoc:
|
3
|
-
module Controller # :nodoc:
|
4
|
-
module HTML # :nodoc: all
|
5
|
-
def self.included(other)
|
6
|
-
other.class_eval do
|
7
|
-
extend ThoughtBot::Shoulda::Controller::HTML::ClassMethods
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
module ClassMethods
|
12
|
-
def controller_name_from_class
|
13
|
-
self.name.gsub(/Test/, '')
|
14
|
-
end
|
15
|
-
|
16
|
-
def make_show_html_tests(res)
|
17
|
-
context "on GET to #{controller_name_from_class}#show" do
|
18
|
-
setup do
|
19
|
-
record = get_existing_record(res)
|
20
|
-
parent_params = make_parent_params(res, record)
|
21
|
-
get :show, parent_params.merge({ res.identifier => record.to_param })
|
22
|
-
end
|
23
|
-
|
24
|
-
if res.denied.actions.include?(:show)
|
25
|
-
should_not_assign_to res.object
|
26
|
-
should_redirect_to res.denied.redirect
|
27
|
-
should_set_the_flash_to res.denied.flash
|
28
|
-
else
|
29
|
-
should_assign_to res.object
|
30
|
-
should_respond_with :success
|
31
|
-
should_render_template :show
|
32
|
-
should_not_set_the_flash
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def make_edit_html_tests(res)
|
38
|
-
context "on GET to #{controller_name_from_class}#edit" do
|
39
|
-
setup do
|
40
|
-
@record = get_existing_record(res)
|
41
|
-
parent_params = make_parent_params(res, @record)
|
42
|
-
get :edit, parent_params.merge({ res.identifier => @record.to_param })
|
43
|
-
end
|
44
|
-
|
45
|
-
if res.denied.actions.include?(:edit)
|
46
|
-
should_not_assign_to res.object
|
47
|
-
should_redirect_to res.denied.redirect
|
48
|
-
should_set_the_flash_to res.denied.flash
|
49
|
-
else
|
50
|
-
should_assign_to res.object
|
51
|
-
should_respond_with :success
|
52
|
-
should_render_template :edit
|
53
|
-
should_not_set_the_flash
|
54
|
-
should_render_a_form
|
55
|
-
should "set @#{res.object} to requested instance" do
|
56
|
-
assert_equal @record, assigns(res.object)
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def make_index_html_tests(res)
|
63
|
-
context "on GET to #{controller_name_from_class}#index" do
|
64
|
-
setup do
|
65
|
-
record = get_existing_record(res) rescue nil
|
66
|
-
parent_params = make_parent_params(res, record)
|
67
|
-
get(:index, parent_params)
|
68
|
-
end
|
69
|
-
|
70
|
-
if res.denied.actions.include?(:index)
|
71
|
-
should_not_assign_to res.object.to_s.pluralize
|
72
|
-
should_redirect_to res.denied.redirect
|
73
|
-
should_set_the_flash_to res.denied.flash
|
74
|
-
else
|
75
|
-
should_respond_with :success
|
76
|
-
should_assign_to res.object.to_s.pluralize
|
77
|
-
should_render_template :index
|
78
|
-
should_not_set_the_flash
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def make_new_html_tests(res)
|
84
|
-
context "on GET to #{controller_name_from_class}#new" do
|
85
|
-
setup do
|
86
|
-
record = get_existing_record(res) rescue nil
|
87
|
-
parent_params = make_parent_params(res, record)
|
88
|
-
get(:new, parent_params)
|
89
|
-
end
|
90
|
-
|
91
|
-
if res.denied.actions.include?(:new)
|
92
|
-
should_not_assign_to res.object
|
93
|
-
should_redirect_to res.denied.redirect
|
94
|
-
should_set_the_flash_to res.denied.flash
|
95
|
-
else
|
96
|
-
should_respond_with :success
|
97
|
-
should_assign_to res.object
|
98
|
-
should_not_set_the_flash
|
99
|
-
should_render_template :new
|
100
|
-
should_render_a_form
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
|
-
def make_destroy_html_tests(res)
|
106
|
-
context "on DELETE to #{controller_name_from_class}#destroy" do
|
107
|
-
setup do
|
108
|
-
@record = get_existing_record(res)
|
109
|
-
parent_params = make_parent_params(res, @record)
|
110
|
-
delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
|
111
|
-
end
|
112
|
-
|
113
|
-
if res.denied.actions.include?(:destroy)
|
114
|
-
should_redirect_to res.denied.redirect
|
115
|
-
should_set_the_flash_to res.denied.flash
|
116
|
-
|
117
|
-
should "not destroy record" do
|
118
|
-
assert_nothing_raised { assert @record.reload }
|
119
|
-
end
|
120
|
-
else
|
121
|
-
should_set_the_flash_to res.destroy.flash
|
122
|
-
if res.destroy.redirect.is_a? Symbol
|
123
|
-
should_respond_with res.destroy.redirect
|
124
|
-
else
|
125
|
-
should_redirect_to res.destroy.redirect
|
126
|
-
end
|
127
|
-
|
128
|
-
should "destroy record" do
|
129
|
-
assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
|
130
|
-
@record.reload
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
def make_create_html_tests(res)
|
138
|
-
context "on POST to #{controller_name_from_class}#create with #{res.create.params.inspect}" do
|
139
|
-
setup do
|
140
|
-
record = get_existing_record(res) rescue nil
|
141
|
-
parent_params = make_parent_params(res, record)
|
142
|
-
@count = res.klass.count
|
143
|
-
post :create, parent_params.merge(res.object => res.create.params)
|
144
|
-
end
|
145
|
-
|
146
|
-
if res.denied.actions.include?(:create)
|
147
|
-
should_redirect_to res.denied.redirect
|
148
|
-
should_set_the_flash_to res.denied.flash
|
149
|
-
should_not_assign_to res.object
|
150
|
-
|
151
|
-
should "not create new record" do
|
152
|
-
assert_equal @count, res.klass.count
|
153
|
-
end
|
154
|
-
else
|
155
|
-
should_assign_to res.object
|
156
|
-
should_set_the_flash_to res.create.flash
|
157
|
-
if res.create.redirect.is_a? Symbol
|
158
|
-
should_respond_with res.create.redirect
|
159
|
-
else
|
160
|
-
should_redirect_to res.create.redirect
|
161
|
-
end
|
162
|
-
|
163
|
-
should "not have errors on @#{res.object}" do
|
164
|
-
assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
def make_update_html_tests(res)
|
171
|
-
context "on PUT to #{controller_name_from_class}#update with #{res.create.params.inspect}" do
|
172
|
-
setup do
|
173
|
-
@record = get_existing_record(res)
|
174
|
-
parent_params = make_parent_params(res, @record)
|
175
|
-
put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
|
176
|
-
end
|
177
|
-
|
178
|
-
if res.denied.actions.include?(:update)
|
179
|
-
should_not_assign_to res.object
|
180
|
-
should_redirect_to res.denied.redirect
|
181
|
-
should_set_the_flash_to res.denied.flash
|
182
|
-
else
|
183
|
-
should_assign_to res.object
|
184
|
-
should_set_the_flash_to(res.update.flash)
|
185
|
-
if res.update.redirect.is_a? Symbol
|
186
|
-
should_respond_with res.update.redirect
|
187
|
-
else
|
188
|
-
should_redirect_to res.update.redirect
|
189
|
-
end
|
190
|
-
|
191
|
-
should "not have errors on @#{res.object}" do
|
192
|
-
assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
@@ -1,170 +0,0 @@
|
|
1
|
-
module ThoughtBot # :nodoc:
|
2
|
-
module Shoulda # :nodoc:
|
3
|
-
module Controller # :nodoc:
|
4
|
-
module XML
|
5
|
-
def self.included(other) #:nodoc:
|
6
|
-
other.class_eval do
|
7
|
-
extend ThoughtBot::Shoulda::Controller::XML::ClassMethods
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
module ClassMethods
|
12
|
-
# Macro that creates a test asserting that the controller responded with an XML content-type
|
13
|
-
# and that the XML contains +<name/>+ as the root element.
|
14
|
-
def should_respond_with_xml_for(name = nil)
|
15
|
-
should "have ContentType set to 'application/xml'" do
|
16
|
-
assert_xml_response
|
17
|
-
end
|
18
|
-
|
19
|
-
if name
|
20
|
-
should "return <#{name}/> as the root element" do
|
21
|
-
body = @response.body.first(100).map {|l| " #{l}"}
|
22
|
-
assert_select name.to_s.dasherize, 1, "Body:\n#{body}...\nDoes not have <#{name}/> as the root element."
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
alias should_respond_with_xml should_respond_with_xml_for
|
27
|
-
|
28
|
-
protected
|
29
|
-
|
30
|
-
def make_show_xml_tests(res) # :nodoc:
|
31
|
-
context "on GET to #{controller_name_from_class}#show as xml" do
|
32
|
-
setup do
|
33
|
-
request_xml
|
34
|
-
record = get_existing_record(res)
|
35
|
-
parent_params = make_parent_params(res, record)
|
36
|
-
get :show, parent_params.merge({ res.identifier => record.to_param })
|
37
|
-
end
|
38
|
-
|
39
|
-
if res.denied.actions.include?(:show)
|
40
|
-
should_not_assign_to res.object
|
41
|
-
should_respond_with 401
|
42
|
-
else
|
43
|
-
should_assign_to res.object
|
44
|
-
should_respond_with :success
|
45
|
-
should_respond_with_xml_for res.object
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def make_edit_xml_tests(res) # :nodoc:
|
51
|
-
# XML doesn't need an :edit action
|
52
|
-
end
|
53
|
-
|
54
|
-
def make_new_xml_tests(res) # :nodoc:
|
55
|
-
# XML doesn't need a :new action
|
56
|
-
end
|
57
|
-
|
58
|
-
def make_index_xml_tests(res) # :nodoc:
|
59
|
-
context "on GET to #{controller_name_from_class}#index as xml" do
|
60
|
-
setup do
|
61
|
-
request_xml
|
62
|
-
parent_params = make_parent_params(res)
|
63
|
-
get(:index, parent_params)
|
64
|
-
end
|
65
|
-
|
66
|
-
if res.denied.actions.include?(:index)
|
67
|
-
should_not_assign_to res.object.to_s.pluralize
|
68
|
-
should_respond_with 401
|
69
|
-
else
|
70
|
-
should_respond_with :success
|
71
|
-
should_respond_with_xml_for res.object.to_s.pluralize
|
72
|
-
should_assign_to res.object.to_s.pluralize
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def make_destroy_xml_tests(res) # :nodoc:
|
78
|
-
context "on DELETE to #{controller_name_from_class}#destroy as xml" do
|
79
|
-
setup do
|
80
|
-
request_xml
|
81
|
-
@record = get_existing_record(res)
|
82
|
-
parent_params = make_parent_params(res, @record)
|
83
|
-
delete :destroy, parent_params.merge({ res.identifier => @record.to_param })
|
84
|
-
end
|
85
|
-
|
86
|
-
if res.denied.actions.include?(:destroy)
|
87
|
-
should_respond_with 401
|
88
|
-
|
89
|
-
should "not destroy record" do
|
90
|
-
assert @record.reload
|
91
|
-
end
|
92
|
-
else
|
93
|
-
should "destroy record" do
|
94
|
-
assert_raises(::ActiveRecord::RecordNotFound, "@#{res.object} was not destroyed.") do
|
95
|
-
@record.reload
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def make_create_xml_tests(res) # :nodoc:
|
103
|
-
context "on POST to #{controller_name_from_class}#create as xml" do
|
104
|
-
setup do
|
105
|
-
request_xml
|
106
|
-
parent_params = make_parent_params(res)
|
107
|
-
@count = res.klass.count
|
108
|
-
post :create, parent_params.merge(res.object => res.create.params)
|
109
|
-
end
|
110
|
-
|
111
|
-
if res.denied.actions.include?(:create)
|
112
|
-
should_respond_with 401
|
113
|
-
should_not_assign_to res.object
|
114
|
-
|
115
|
-
should "not create new record" do
|
116
|
-
assert_equal @count, res.klass.count
|
117
|
-
end
|
118
|
-
else
|
119
|
-
should_assign_to res.object
|
120
|
-
|
121
|
-
should "not have errors on @#{res.object}" do
|
122
|
-
assert_equal [], pretty_error_messages(assigns(res.object)), "@#{res.object} has errors:"
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
def make_update_xml_tests(res) # :nodoc:
|
129
|
-
context "on PUT to #{controller_name_from_class}#update as xml" do
|
130
|
-
setup do
|
131
|
-
request_xml
|
132
|
-
@record = get_existing_record(res)
|
133
|
-
parent_params = make_parent_params(res, @record)
|
134
|
-
put :update, parent_params.merge(res.identifier => @record.to_param, res.object => res.update.params)
|
135
|
-
end
|
136
|
-
|
137
|
-
if res.denied.actions.include?(:update)
|
138
|
-
should_not_assign_to res.object
|
139
|
-
should_respond_with 401
|
140
|
-
else
|
141
|
-
should_assign_to res.object
|
142
|
-
|
143
|
-
should "not have errors on @#{res.object}" do
|
144
|
-
assert_equal [], assigns(res.object).errors.full_messages, "@#{res.object} has errors:"
|
145
|
-
end
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
# Sets the next request's format to 'application/xml'
|
152
|
-
def request_xml
|
153
|
-
@request.accept = "application/xml"
|
154
|
-
end
|
155
|
-
|
156
|
-
# Asserts that the controller's response was 'application/xml'
|
157
|
-
def assert_xml_response
|
158
|
-
content_type = (@response.headers["Content-Type"] || @response.headers["type"]).to_s
|
159
|
-
regex = %r{\bapplication/xml\b}
|
160
|
-
|
161
|
-
msg = "Content Type '#{content_type.inspect}' doesn't match '#{regex.inspect}'\n"
|
162
|
-
msg += "Body: #{@response.body.first(100).chomp} ..."
|
163
|
-
|
164
|
-
assert_match regex, content_type, msg
|
165
|
-
end
|
166
|
-
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# Stolen straight from ActiveSupport
|
2
|
-
|
3
|
-
class Proc #:nodoc:
|
4
|
-
def bind(object)
|
5
|
-
block, time = self, Time.now
|
6
|
-
(class << object; self end).class_eval do
|
7
|
-
method_name = "__bind_#{time.to_i}_#{time.usec}"
|
8
|
-
define_method(method_name, &block)
|
9
|
-
method = instance_method(method_name)
|
10
|
-
remove_method(method_name)
|
11
|
-
method
|
12
|
-
end.bind(object)
|
13
|
-
end
|
14
|
-
end
|
@@ -1,239 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'proc_extensions')
|
2
|
-
|
3
|
-
module Thoughtbot
|
4
|
-
module Shoulda
|
5
|
-
class << self
|
6
|
-
attr_accessor :current_context
|
7
|
-
end
|
8
|
-
|
9
|
-
VERSION = '1.1.1'
|
10
|
-
|
11
|
-
# = Should statements
|
12
|
-
#
|
13
|
-
# Should statements are just syntactic sugar over normal Test::Unit test methods. A should block
|
14
|
-
# contains all the normal code and assertions you're used to seeing, with the added benefit that
|
15
|
-
# they can be wrapped inside context blocks (see below).
|
16
|
-
#
|
17
|
-
# == Example:
|
18
|
-
#
|
19
|
-
# class UserTest << Test::Unit::TestCase
|
20
|
-
#
|
21
|
-
# def setup
|
22
|
-
# @user = User.new("John", "Doe")
|
23
|
-
# end
|
24
|
-
#
|
25
|
-
# should "return its full name"
|
26
|
-
# assert_equal 'John Doe', @user.full_name
|
27
|
-
# end
|
28
|
-
#
|
29
|
-
# end
|
30
|
-
#
|
31
|
-
# ...will produce the following test:
|
32
|
-
# * <tt>"test: User should return its full name. "</tt>
|
33
|
-
#
|
34
|
-
# Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
|
35
|
-
|
36
|
-
def should(name, &blk)
|
37
|
-
if Shoulda.current_context
|
38
|
-
Shoulda.current_context.should(name, &blk)
|
39
|
-
else
|
40
|
-
context_name = self.name.gsub(/Test/, "")
|
41
|
-
context = Thoughtbot::Shoulda::Context.new(context_name, self) do
|
42
|
-
should(name, &blk)
|
43
|
-
end
|
44
|
-
context.build
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
# Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
|
49
|
-
def should_eventually(name, &blk)
|
50
|
-
context_name = self.name.gsub(/Test/, "")
|
51
|
-
context = Thoughtbot::Shoulda::Context.new(context_name, self) do
|
52
|
-
should_eventually(name, &blk)
|
53
|
-
end
|
54
|
-
context.build
|
55
|
-
end
|
56
|
-
|
57
|
-
# = Contexts
|
58
|
-
#
|
59
|
-
# A context block groups should statements under a common set of setup/teardown methods.
|
60
|
-
# Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
|
61
|
-
# and readability of your test code.
|
62
|
-
#
|
63
|
-
# A context block can contain setup, should, should_eventually, and teardown blocks.
|
64
|
-
#
|
65
|
-
# class UserTest << Test::Unit::TestCase
|
66
|
-
# context "A User instance" do
|
67
|
-
# setup do
|
68
|
-
# @user = User.find(:first)
|
69
|
-
# end
|
70
|
-
#
|
71
|
-
# should "return its full name"
|
72
|
-
# assert_equal 'John Doe', @user.full_name
|
73
|
-
# end
|
74
|
-
# end
|
75
|
-
# end
|
76
|
-
#
|
77
|
-
# This code will produce the method <tt>"test: A User instance should return its full name. "</tt>.
|
78
|
-
#
|
79
|
-
# Contexts may be nested. Nested contexts run their setup blocks from out to in before each
|
80
|
-
# should statement. They then run their teardown blocks from in to out after each should statement.
|
81
|
-
#
|
82
|
-
# class UserTest << Test::Unit::TestCase
|
83
|
-
# context "A User instance" do
|
84
|
-
# setup do
|
85
|
-
# @user = User.find(:first)
|
86
|
-
# end
|
87
|
-
#
|
88
|
-
# should "return its full name"
|
89
|
-
# assert_equal 'John Doe', @user.full_name
|
90
|
-
# end
|
91
|
-
#
|
92
|
-
# context "with a profile" do
|
93
|
-
# setup do
|
94
|
-
# @user.profile = Profile.find(:first)
|
95
|
-
# end
|
96
|
-
#
|
97
|
-
# should "return true when sent :has_profile?"
|
98
|
-
# assert @user.has_profile?
|
99
|
-
# end
|
100
|
-
# end
|
101
|
-
# end
|
102
|
-
# end
|
103
|
-
#
|
104
|
-
# This code will produce the following methods
|
105
|
-
# * <tt>"test: A User instance should return its full name. "</tt>
|
106
|
-
# * <tt>"test: A User instance with a profile should return true when sent :has_profile?. "</tt>
|
107
|
-
#
|
108
|
-
# <b>Just like should statements, a context block can exist next to normal <tt>def test_the_old_way; end</tt>
|
109
|
-
# tests</b>. This means you do not have to fully commit to the context/should syntax in a test file.
|
110
|
-
|
111
|
-
def context(name, &blk)
|
112
|
-
if Shoulda.current_context
|
113
|
-
Shoulda.current_context.context(name, &blk)
|
114
|
-
else
|
115
|
-
context = Thoughtbot::Shoulda::Context.new(name, self, &blk)
|
116
|
-
context.build
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
class Context # :nodoc:
|
121
|
-
|
122
|
-
attr_accessor :name # my name
|
123
|
-
attr_accessor :parent # may be another context, or the original test::unit class.
|
124
|
-
attr_accessor :subcontexts # array of contexts nested under myself
|
125
|
-
attr_accessor :setup_block # block given via a setup method
|
126
|
-
attr_accessor :teardown_block # block given via a teardown method
|
127
|
-
attr_accessor :shoulds # array of hashes representing the should statements
|
128
|
-
attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
|
129
|
-
|
130
|
-
def initialize(name, parent, &blk)
|
131
|
-
Shoulda.current_context = self
|
132
|
-
self.name = name
|
133
|
-
self.parent = parent
|
134
|
-
self.setup_block = nil
|
135
|
-
self.teardown_block = nil
|
136
|
-
self.shoulds = []
|
137
|
-
self.should_eventuallys = []
|
138
|
-
self.subcontexts = []
|
139
|
-
|
140
|
-
blk.bind(self).call
|
141
|
-
Shoulda.current_context = nil
|
142
|
-
end
|
143
|
-
|
144
|
-
def context(name, &blk)
|
145
|
-
subcontexts << Context.new(name, self, &blk)
|
146
|
-
Shoulda.current_context = self
|
147
|
-
end
|
148
|
-
|
149
|
-
def setup(&blk)
|
150
|
-
self.setup_block = blk
|
151
|
-
end
|
152
|
-
|
153
|
-
def teardown(&blk)
|
154
|
-
self.teardown_block = blk
|
155
|
-
end
|
156
|
-
|
157
|
-
def should(name, &blk)
|
158
|
-
self.shoulds << { :name => name, :block => blk }
|
159
|
-
end
|
160
|
-
|
161
|
-
def should_eventually(name, &blk)
|
162
|
-
self.should_eventuallys << { :name => name, :block => blk }
|
163
|
-
end
|
164
|
-
|
165
|
-
def full_name
|
166
|
-
parent_name = parent.full_name if am_subcontext?
|
167
|
-
return [parent_name, name].join(" ").strip
|
168
|
-
end
|
169
|
-
|
170
|
-
def am_subcontext?
|
171
|
-
parent.is_a?(self.class) # my parent is the same class as myself.
|
172
|
-
end
|
173
|
-
|
174
|
-
def test_unit_class
|
175
|
-
am_subcontext? ? parent.test_unit_class : parent
|
176
|
-
end
|
177
|
-
|
178
|
-
def create_test_from_should_hash(should)
|
179
|
-
test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
|
180
|
-
|
181
|
-
if test_unit_class.instance_methods.include?(test_name.to_s)
|
182
|
-
warn " * WARNING: '#{test_name}' is already defined"
|
183
|
-
end
|
184
|
-
|
185
|
-
context = self
|
186
|
-
test_unit_class.send(:define_method, test_name) do |*args|
|
187
|
-
begin
|
188
|
-
context.run_all_setup_blocks(self)
|
189
|
-
should[:block].bind(self).call
|
190
|
-
ensure
|
191
|
-
context.run_all_teardown_blocks(self)
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
195
|
-
|
196
|
-
def run_all_setup_blocks(binding)
|
197
|
-
self.parent.run_all_setup_blocks(binding) if am_subcontext?
|
198
|
-
setup_block.bind(binding).call if setup_block
|
199
|
-
end
|
200
|
-
|
201
|
-
def run_all_teardown_blocks(binding)
|
202
|
-
teardown_block.bind(binding).call if teardown_block
|
203
|
-
self.parent.run_all_teardown_blocks(binding) if am_subcontext?
|
204
|
-
end
|
205
|
-
|
206
|
-
def print_should_eventuallys
|
207
|
-
should_eventuallys.each do |should|
|
208
|
-
test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
|
209
|
-
puts " * DEFERRED: " + test_name
|
210
|
-
end
|
211
|
-
subcontexts.each { |context| context.print_should_eventuallys }
|
212
|
-
end
|
213
|
-
|
214
|
-
def build
|
215
|
-
shoulds.each do |should|
|
216
|
-
create_test_from_should_hash(should)
|
217
|
-
end
|
218
|
-
|
219
|
-
subcontexts.each { |context| context.build }
|
220
|
-
|
221
|
-
print_should_eventuallys
|
222
|
-
end
|
223
|
-
|
224
|
-
def method_missing(method, *args, &blk)
|
225
|
-
test_unit_class.send(method, *args, &blk)
|
226
|
-
end
|
227
|
-
|
228
|
-
end
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
module Test # :nodoc: all
|
233
|
-
module Unit
|
234
|
-
class TestCase
|
235
|
-
extend Thoughtbot::Shoulda
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|