jakewendt-simply_testable 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ module SimplyTestable::ActionControllerExtension
2
+ module TestCase
3
+
4
+ def turn_https_on
5
+ @request.env['HTTPS'] = 'on'
6
+ # @request.env['HTTP_X_FORWARDED_PROTO'] == 'https'
7
+ end
8
+
9
+ def turn_https_off
10
+ @request.env['HTTPS'] = nil
11
+ end
12
+
13
+ def assert_layout(layout)
14
+ layout = "layouts/#{layout}" unless layout.match(/^layouts/)
15
+ assert_equal layout, @response.layout
16
+ end
17
+
18
+ end # module TestCase
19
+ end # module SimplyTestable::ActionControllerExtension
20
+ require 'action_controller'
21
+ require 'action_controller/test_case'
22
+ ActionController::TestCase.send(:include,
23
+ SimplyTestable::ActionControllerExtension::TestCase)
@@ -0,0 +1,41 @@
1
+ module SimplyTestable::ActsAsList
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ # base.send(:include,InstanceMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def assert_should_act_as_list(*args)
11
+ options = args.extract_options!
12
+ scope = options[:scope]
13
+
14
+ test "#{brand}should act as list" do
15
+ model = create_object.class.name
16
+ model.constantize.destroy_all
17
+ object = create_object
18
+ assert_equal 1, object.position
19
+ attrs = {}
20
+ Array(scope).each do |attr|
21
+ attrs[attr.to_sym] = object.send(attr)
22
+ end if scope
23
+ object = create_object(attrs)
24
+ assert_equal 2, object.position
25
+
26
+ # gotta be a relative test as there may already
27
+ # by existing objects (unless I destroy them)
28
+ assert_difference("#{model}.last.position",1) do
29
+ create_object(attrs)
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end # module SimplyTestable::ActsAsList
38
+ require 'active_support'
39
+ require 'active_support/test_case'
40
+ ActiveSupport::TestCase.send(:include,
41
+ SimplyTestable::ActsAsList)
@@ -0,0 +1,49 @@
1
+ module SimplyTestable::Assertions
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.send(:include,InstanceMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ end # ClassMethods
11
+
12
+ module InstanceMethods
13
+
14
+ # basically a copy of assert_difference, but
15
+ # without any explicit comparison as it is
16
+ # simply stating that something will change
17
+ # (designed for updated_at)
18
+ def assert_changes(expression, message = nil, &block)
19
+ b = block.send(:binding)
20
+ exps = Array.wrap(expression)
21
+ before = exps.map { |e| eval(e, b) }
22
+ yield
23
+ exps.each_with_index do |e, i|
24
+ error = "#{e.inspect} didn't change"
25
+ error = "#{message}.\n#{error}" if message
26
+ assert_not_equal(before[i], eval(e, b), error)
27
+ end
28
+ end
29
+
30
+ # Just a negation of assert_changes
31
+ def deny_changes(expression, message = nil, &block)
32
+ b = block.send(:binding)
33
+ exps = Array.wrap(expression)
34
+ before = exps.map { |e| eval(e, b) }
35
+ yield
36
+ exps.each_with_index do |e, i|
37
+ error = "#{e.inspect} changed"
38
+ error = "#{message}.\n#{error}" if message
39
+ assert_equal(before[i], eval(e, b), error)
40
+ end
41
+ end
42
+
43
+ end # InstanceMethods
44
+
45
+ end # module SimplyTestable::Assertions
46
+ require 'active_support'
47
+ require 'active_support/test_case'
48
+ ActiveSupport::TestCase.send(:include,
49
+ SimplyTestable::Assertions)
@@ -0,0 +1,222 @@
1
+ module SimplyTestable::Associations
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ # base.send(:include,InstanceMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def assert_should_initially_belong_to(*associations)
11
+ options = associations.extract_options!
12
+ model = options[:model] || st_model_name
13
+
14
+ associations.each do |assoc|
15
+ class_name = ( assoc = assoc.to_s ).camelize
16
+
17
+ title = "#{brand}should initially belong to #{assoc}"
18
+ if !options[:class_name].blank?
19
+ title << " ( #{options[:class_name]} )"
20
+ class_name = options[:class_name].to_s
21
+ end
22
+ test title do
23
+ object = create_object
24
+ assert_not_nil object.send(assoc)
25
+ if object.send(assoc).respond_to?(
26
+ "#{model.underscore.pluralize}_count")
27
+ assert_equal 1, object.reload.send(assoc).send(
28
+ "#{model.underscore.pluralize}_count")
29
+ end
30
+ if !options[:class_name].blank?
31
+ assert object.send(assoc).is_a?(class_name.constantize)
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ def assert_should_belong_to(*associations)
40
+ options = associations.extract_options!
41
+ model = options[:model] || st_model_name
42
+
43
+ associations.each do |assoc|
44
+ class_name = ( assoc = assoc.to_s ).camelize
45
+ title = "#{brand}should belong to #{assoc}"
46
+ # if !options[:as].blank?
47
+ # title << " as #{options[:as]}"
48
+ # as = options[:as]
49
+ # end
50
+ if !options[:class_name].blank?
51
+ title << " ( #{options[:class_name]} )"
52
+ class_name = options[:class_name].to_s
53
+ end
54
+ test title do
55
+ object = create_object
56
+ assert_nil object.send(assoc)
57
+ object.send("#{assoc}=",send("create_#{class_name.underscore}"))
58
+ assert_not_nil object.send(assoc)
59
+ assert object.send(assoc).is_a?(class_name.constantize
60
+ ) unless options[:polymorphic]
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+
67
+ def assert_should_have_one(*associations)
68
+ options = associations.extract_options!
69
+ model = options[:model] || st_model_name
70
+
71
+ # foreign_key = if !options[:foreign_key].blank?
72
+ # options[:foreign_key].to_sym
73
+ # else
74
+ # "#{model.underscore}_id".to_sym
75
+ # end
76
+
77
+ associations.each do |assoc|
78
+ assoc = assoc.to_s
79
+
80
+ test "#{brand}should have one #{assoc}" do
81
+ object = create_object
82
+ assert_nil object.reload.send(assoc)
83
+ # send("create_#{assoc}", foreign_key => object.id)
84
+ send("create_#{assoc}", model.underscore => object )
85
+ assert_not_nil object.reload.send(assoc)
86
+ object.send(assoc).destroy
87
+ assert_nil object.reload.send(assoc)
88
+ end
89
+
90
+ end
91
+
92
+ end
93
+
94
+ def assert_should_have_many_(*associations)
95
+ options = associations.extract_options!
96
+ model = options[:model] || st_model_name
97
+
98
+ # foreign_key = if !options[:foreign_key].blank?
99
+ # options[:foreign_key].to_sym
100
+ # else
101
+ # "#{model.underscore}_id".to_sym
102
+ # end
103
+
104
+ associations.each do |assoc|
105
+ class_name = ( assoc = assoc.to_s ).camelize
106
+
107
+ title = "#{brand}should have many #{assoc}"
108
+ if !options[:class_name].blank?
109
+ title << " ( #{options[:class_name]} )"
110
+ class_name = options[:class_name].to_s
111
+ end
112
+ test title do
113
+ object = create_object
114
+ assert_equal 0, object.send(assoc).length
115
+ command = ["create_#{class_name.singularize.underscore}"]
116
+ if !options[:foreign_key].blank?
117
+ command.push( options[:foreign_key].to_sym => object.id )
118
+ else
119
+ command.push( model.underscore => object )
120
+ end
121
+ # send("create_#{class_name.singularize.underscore}", foreign_key => object.id)
122
+ # send("create_#{class_name.singularize.underscore}", model.underscore => object )
123
+ send *command
124
+ assert_equal 1, object.reload.send(assoc).length
125
+ if object.respond_to?("#{assoc}_count")
126
+ assert_equal 1, object.reload.send("#{assoc}_count")
127
+ end
128
+ # send("create_#{class_name.singularize.underscore}", foreign_key => object.id)
129
+ # send("create_#{class_name.singularize.underscore}", model.underscore => object )
130
+ send *command
131
+ assert_equal 2, object.reload.send(assoc).length
132
+ if object.respond_to?("#{assoc}_count")
133
+ assert_equal 2, object.reload.send("#{assoc}_count")
134
+ end
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+ alias_method :assert_should_have_many,
141
+ :assert_should_have_many_
142
+ alias_method :assert_should_have_many_associations,
143
+ :assert_should_have_many_
144
+
145
+ def assert_should_habtm(*associations)
146
+ options = associations.extract_options!
147
+ model = options[:model] || st_model_name
148
+
149
+ associations.each do |assoc|
150
+ assoc = assoc.to_s
151
+
152
+ test "#{brand}should habtm #{assoc}" do
153
+ object = create_object
154
+ assert_equal 0, object.send(assoc).length
155
+ object.send(assoc) << send("create_#{assoc.singularize}")
156
+ assert_equal 1, object.reload.send(assoc).length
157
+ if object.respond_to?("#{assoc}_count")
158
+ assert_equal 1, object.reload.send("#{assoc}_count")
159
+ end
160
+ object.send(assoc) << send("create_#{assoc.singularize}")
161
+ assert_equal 2, object.reload.send(assoc).length
162
+ if object.respond_to?("#{assoc}_count")
163
+ assert_equal 2, object.reload.send("#{assoc}_count")
164
+ end
165
+ end
166
+
167
+ end
168
+
169
+ end
170
+
171
+ def assert_requires_valid_associations(*associations)
172
+ # options = associations.extract_options!
173
+ # model = options[:model] || st_model_name
174
+ #
175
+ # associations.each do |assoc|
176
+ # as = assoc = assoc.to_s
177
+ # as = options[:as] if !options[:as].blank?
178
+ #
179
+ # test "#{brand}should require foreign key #{as}_id" do
180
+ # assert_difference("#{model}.count",0) do
181
+ # object = create_object("#{as}_id".to_sym => nil)
182
+ # assert object.errors.on("#{as}_id".to_sym)
183
+ # end
184
+ # end
185
+ #
186
+ # # test "#{brand}should require valid foreign key #{as}_id" do
187
+ # # assert_difference("#{model}.count",0) do
188
+ # # object = create_object("#{as}_id".to_sym => 0)
189
+ # # assert object.errors.on("#{as}_id".to_sym)
190
+ # # end
191
+ # # end
192
+ #
193
+ # title = "#{brand}should require valid association #{assoc}"
194
+ # title << " as #{options[:as]}" if !options[:as].blank?
195
+ # test title do
196
+ # assert_difference("#{model}.count",0) {
197
+ # object = create_object(
198
+ # assoc.to_sym => Factory.build(assoc.to_sym))
199
+ # # as.to_sym => Factory.build(assoc.to_sym))
200
+ # assert object.errors.on("#{as}_id".to_sym)
201
+ # }
202
+ # end
203
+ #
204
+ # end
205
+
206
+ end
207
+ alias_method :assert_should_require_valid_associations,
208
+ :assert_requires_valid_associations
209
+ alias_method :assert_should_require_valid_association,
210
+ :assert_requires_valid_associations
211
+ alias_method :assert_requires_valid_association,
212
+ :assert_requires_valid_associations
213
+ alias_method :assert_requires_valid,
214
+ :assert_requires_valid_associations
215
+
216
+ end # ClassMethods
217
+
218
+ end # module SimplyTestable::Associations
219
+ require 'active_support'
220
+ require 'active_support/test_case'
221
+ ActiveSupport::TestCase.send(:include,
222
+ SimplyTestable::Associations)
@@ -0,0 +1,240 @@
1
+ module SimplyTestable::Attributes
2
+
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ # base.send(:include,InstanceMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def assert_should_require_unique_attribute(*attributes)
11
+ options = attributes.extract_options!
12
+ model = options[:model] || st_model_name
13
+
14
+ attributes.each do |attr|
15
+ attr = attr.to_s
16
+ title = "#{brand}should require unique #{attr}"
17
+ scope = options[:scope]
18
+ unless scope.blank?
19
+ title << " scope "
20
+ title << (( scope.is_a?(Array) )?scope.join(','):scope.to_s)
21
+ end
22
+ test title do
23
+ o = create_object
24
+ assert_no_difference "#{model}.count" do
25
+ attrs = { attr.to_sym => o.send(attr) }
26
+ if( scope.is_a?(String) || scope.is_a?(Symbol) )
27
+ attrs[scope.to_sym] = o.send(scope.to_sym)
28
+ elsif scope.is_a?(Array)
29
+ scope.each do |s|
30
+ attrs[s.to_sym] = o.send(s.to_sym)
31
+ end
32
+ end
33
+ object = create_object(attrs)
34
+ assert object.errors.on_attr_and_type(attr.to_sym, :taken)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ alias_method :assert_should_require_unique_attributes,
40
+ :assert_should_require_unique_attribute
41
+ alias_method :assert_should_require_unique,
42
+ :assert_should_require_unique_attribute
43
+
44
+ def assert_should_require_attribute_not_nil(*attributes)
45
+ options = attributes.extract_options!
46
+ model = options[:model] || st_model_name
47
+
48
+ attributes.each do |attr|
49
+ attr = attr.to_s
50
+ test "#{brand}should require #{attr} not nil" do
51
+ assert_no_difference "#{model}.count" do
52
+ object = create_object(attr.to_sym => nil)
53
+ assert object.errors.on(attr.to_sym)
54
+ end
55
+ end
56
+ end
57
+ end
58
+ alias_method :assert_should_require_attributes_not_nil,
59
+ :assert_should_require_attribute_not_nil
60
+ alias_method :assert_should_require_not_nil,
61
+ :assert_should_require_attribute_not_nil
62
+
63
+ def assert_should_require_attribute(*attributes)
64
+ options = attributes.extract_options!
65
+ model = options[:model] || st_model_name
66
+
67
+ attributes.each do |attr|
68
+ attr = attr.to_s
69
+ test "#{brand}should require #{attr}" do
70
+ assert_no_difference "#{model}.count" do
71
+ object = create_object(attr.to_sym => nil)
72
+ assert object.errors.on_attr_and_type(attr.to_sym, :blank) ||
73
+ object.errors.on_attr_and_type(attr.to_sym, :too_short)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ alias_method :assert_should_require_attributes,
79
+ :assert_should_require_attribute
80
+ alias_method :assert_should_require,
81
+ :assert_should_require_attribute
82
+
83
+ def assert_should_not_require_attribute(*attributes)
84
+ options = attributes.extract_options!
85
+ model = options[:model] || st_model_name
86
+
87
+ attributes.each do |attr|
88
+ attr = attr.to_s
89
+ test "#{brand}should not require #{attr}" do
90
+ assert_difference( "#{model}.count", 1 ) do
91
+ object = create_object(attr.to_sym => nil)
92
+ assert !object.errors.on(attr.to_sym)
93
+ if attr =~ /^(.*)_id$/
94
+ assert !object.errors.on($1.to_sym)
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+ alias_method :assert_should_not_require_attributes,
101
+ :assert_should_not_require_attribute
102
+ alias_method :assert_should_not_require,
103
+ :assert_should_not_require_attribute
104
+
105
+ def assert_should_require_attribute_length(*attributes)
106
+ options = attributes.extract_options!
107
+ model = options[:model] || st_model_name
108
+
109
+ if( ( options.keys & [:in,:within] ).length >= 1 )
110
+ range = options[:in]||options[:within]
111
+ options[:minimum] = range.min
112
+ options[:maximum] = range.max
113
+ end
114
+
115
+ attributes.each do |attr|
116
+ attr = attr.to_s
117
+ if options.keys.include?(:is)
118
+ length = options[:is]
119
+ test "#{brand}should require exact length of #{length} for #{attr}" do
120
+ assert_no_difference "#{model}.count" do
121
+ value = 'x'*(length-1)
122
+ object = create_object(attr.to_sym => value)
123
+ assert_equal length-1, object.send(attr.to_sym).length
124
+ assert_equal object.send(attr.to_sym), value
125
+ assert object.errors.on_attr_and_type(attr.to_sym, :wrong_length)
126
+ end
127
+ assert_no_difference "#{model}.count" do
128
+ value = 'x'*(length+1)
129
+ object = create_object(attr.to_sym => value)
130
+ assert_equal length+1, object.send(attr.to_sym).length
131
+ assert_equal object.send(attr.to_sym), value
132
+ assert object.errors.on_attr_and_type(attr.to_sym, :wrong_length)
133
+ end
134
+ end
135
+ end
136
+
137
+ if options.keys.include?(:minimum)
138
+ min = options[:minimum]
139
+ test "#{brand}should require min length of #{min} for #{attr}" do
140
+ # because the model may have other requirements
141
+ # just check to ensure that we don't get a :too_short error
142
+ # assert_difference "#{model}.count" do
143
+ value = 'x'*(min)
144
+ object = create_object(attr.to_sym => value)
145
+ assert_equal min, object.send(attr.to_sym).length
146
+ assert_equal object.send(attr.to_sym), value
147
+ assert !object.errors.on_attr_and_type(attr.to_sym, :too_short)
148
+ # end
149
+ assert_no_difference "#{model}.count" do
150
+ value = 'x'*(min-1)
151
+ object = create_object(attr.to_sym => value)
152
+ assert_equal min-1, object.send(attr.to_sym).length
153
+ assert_equal object.send(attr.to_sym), value
154
+ assert object.errors.on_attr_and_type(attr.to_sym, :too_short)
155
+ end
156
+ end
157
+ end
158
+
159
+ if options.keys.include?(:maximum)
160
+ max = options[:maximum]
161
+ test "#{brand}should require max length of #{max} for #{attr}" do
162
+ # because the model may have other requirements
163
+ # just check to ensure that we don't get a :too_long error
164
+ # assert_difference "#{model}.count" do
165
+ value = 'x'*(max)
166
+ object = create_object(attr.to_sym => value)
167
+ assert_equal max, object.send(attr.to_sym).length
168
+ assert_equal object.send(attr.to_sym), value
169
+ assert !object.errors.on_attr_and_type(attr.to_sym, :too_long)
170
+ # end
171
+ assert_no_difference "#{model}.count" do
172
+ value = 'x'*(max+1)
173
+ object = create_object(attr.to_sym => value)
174
+ assert_equal max+1, object.send(attr.to_sym).length
175
+ assert_equal object.send(attr.to_sym), value
176
+ assert object.errors.on_attr_and_type(attr.to_sym, :too_long)
177
+ end
178
+ end
179
+ end
180
+
181
+ end
182
+ end
183
+ alias_method :assert_should_require_attributes_length,
184
+ :assert_should_require_attribute_length
185
+ alias_method :assert_should_require_length,
186
+ :assert_should_require_attribute_length
187
+
188
+ def assert_should_protect_attribute(*attributes)
189
+ options = attributes.extract_options!
190
+ model_name = options[:model] || st_model_name
191
+ model = model_name.constantize
192
+
193
+ attributes.each do |attr|
194
+ attr = attr.to_s
195
+ test "#{brand}should protect attribute #{attr}" do
196
+ assert model.accessible_attributes||model.protected_attributes,
197
+ "Both accessible and protected attributes are empty"
198
+ assert !(model.accessible_attributes||[]).include?(attr),
199
+ "#{attr} is included in accessible attributes"
200
+ if !model.protected_attributes.nil?
201
+ assert model.protected_attributes.include?(attr),
202
+ "#{attr} is not included in protected attributes"
203
+ end
204
+ end
205
+ end
206
+ end
207
+ alias_method :assert_should_protect_attributes,
208
+ :assert_should_protect_attribute
209
+ alias_method :assert_should_protect,
210
+ :assert_should_protect_attribute
211
+
212
+ def assert_should_not_protect_attribute(*attributes)
213
+ options = attributes.extract_options!
214
+ model_name = options[:model] || st_model_name
215
+ model = model_name.constantize
216
+
217
+ attributes.each do |attr|
218
+ attr = attr.to_s
219
+ test "#{brand}should not protect attribute #{attr}" do
220
+ assert !(model.protected_attributes||[]).include?(attr),
221
+ "#{attr} is included in protected attributes"
222
+ if !model.accessible_attributes.nil?
223
+ assert model.accessible_attributes.include?(attr),
224
+ "#{attr} is not included in accessible attributes"
225
+ end
226
+ end
227
+ end
228
+ end
229
+ alias_method :assert_should_not_protect_attributes,
230
+ :assert_should_not_protect_attribute
231
+ alias_method :assert_should_not_protect,
232
+ :assert_should_not_protect_attribute
233
+
234
+ end
235
+
236
+ end # module SimplyTestable::Attributes
237
+ require 'active_support'
238
+ require 'active_support/test_case'
239
+ ActiveSupport::TestCase.send(:include,
240
+ SimplyTestable::Attributes)