Module: SimplyTestable::Attributes::ClassMethods

Defined in:
lib/simply_testable/attributes.rb

Instance Method Summary collapse

Instance Method Details

#assert_should_not_protect_attribute(*attributes) ⇒ Object Also known as: assert_should_not_protect_attributes, assert_should_not_protect



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/simply_testable/attributes.rb', line 212

def assert_should_not_protect_attribute(*attributes)
	options = attributes.extract_options!
	model_name = options[:model] || st_model_name
	model = model_name.constantize
	
	attributes.each do |attr|
		attr = attr.to_s
		test "#{brand}should not protect attribute #{attr}" do
			assert !(model.protected_attributes||[]).include?(attr),
				"#{attr} is included in protected attributes"
			if !model.accessible_attributes.nil?
				assert model.accessible_attributes.include?(attr),
					"#{attr} is not included in accessible attributes"
			end
		end
	end
end

#assert_should_not_require_attribute(*attributes) ⇒ Object Also known as: assert_should_not_require_attributes, assert_should_not_require



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/simply_testable/attributes.rb', line 83

def assert_should_not_require_attribute(*attributes)
	options = attributes.extract_options!
	model = options[:model] || st_model_name
	
	attributes.each do |attr|
		attr = attr.to_s
		test "#{brand}should not require #{attr}" do
			assert_difference( "#{model}.count", 1 ) do
				object = create_object(attr.to_sym => nil)
				assert !object.errors.on(attr.to_sym)
				if attr =~ /^(.*)_id$/
					assert !object.errors.on($1.to_sym)
				end
			end
		end
	end
end

#assert_should_protect_attribute(*attributes) ⇒ Object Also known as: assert_should_protect_attributes, assert_should_protect



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/simply_testable/attributes.rb', line 188

def assert_should_protect_attribute(*attributes)
	options = attributes.extract_options!
	model_name = options[:model] || st_model_name
	model = model_name.constantize
	
	attributes.each do |attr|
		attr = attr.to_s
		test "#{brand}should protect attribute #{attr}" do
			assert model.accessible_attributes||model.protected_attributes,
				"Both accessible and protected attributes are empty"
			assert !(model.accessible_attributes||[]).include?(attr),
				"#{attr} is included in accessible attributes"
			if !model.protected_attributes.nil?
				assert model.protected_attributes.include?(attr),
					"#{attr} is not included in protected attributes"
			end
		end
	end
end

#assert_should_require_attribute(*attributes) ⇒ Object Also known as: assert_should_require_attributes, assert_should_require



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/simply_testable/attributes.rb', line 63

def assert_should_require_attribute(*attributes)
	options = attributes.extract_options!
	model = options[:model] || st_model_name
	
	attributes.each do |attr|
		attr = attr.to_s
		test "#{brand}should require #{attr}" do
			assert_no_difference "#{model}.count" do
				object = create_object(attr.to_sym => nil)
				assert object.errors.on_attr_and_type(attr.to_sym, :blank) ||
					object.errors.on_attr_and_type(attr.to_sym, :too_short)
			end
		end
	end
end

#assert_should_require_attribute_length(*attributes) ⇒ Object Also known as: assert_should_require_attributes_length, assert_should_require_length



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
# File 'lib/simply_testable/attributes.rb', line 105

def assert_should_require_attribute_length(*attributes)
	options = attributes.extract_options!
	model = options[:model] || st_model_name

	if( ( options.keys & [:in,:within] ).length >= 1 )
		range = options[:in]||options[:within]
		options[:minimum] = range.min
		options[:maximum] = range.max
	end
	
	attributes.each do |attr|
		attr = attr.to_s
		if options.keys.include?(:is)
			length = options[:is]
			test "#{brand}should require exact length of #{length} for #{attr}" do
				assert_no_difference "#{model}.count" do
					value = 'x'*(length-1)
					object = create_object(attr.to_sym => value)
					assert_equal length-1, object.send(attr.to_sym).length
					assert_equal object.send(attr.to_sym), value
					assert object.errors.on_attr_and_type(attr.to_sym, :wrong_length)
				end
				assert_no_difference "#{model}.count" do
					value = 'x'*(length+1)
					object = create_object(attr.to_sym => value)
					assert_equal length+1, object.send(attr.to_sym).length
					assert_equal object.send(attr.to_sym), value
					assert object.errors.on_attr_and_type(attr.to_sym, :wrong_length)
				end
			end
		end

		if options.keys.include?(:minimum)
			min = options[:minimum]
			test "#{brand}should require min length of #{min} for #{attr}" do
#	because the model may have other requirements
#	just check to ensure that we don't get a :too_short error
#						assert_difference "#{model}.count" do
					value = 'x'*(min)
					object = create_object(attr.to_sym => value)
					assert_equal min, object.send(attr.to_sym).length
					assert_equal object.send(attr.to_sym), value
					assert !object.errors.on_attr_and_type(attr.to_sym, :too_short)
#						end
				assert_no_difference "#{model}.count" do
					value = 'x'*(min-1)
					object = create_object(attr.to_sym => value)
					assert_equal min-1, object.send(attr.to_sym).length
					assert_equal object.send(attr.to_sym), value
					assert object.errors.on_attr_and_type(attr.to_sym, :too_short)
				end
			end
		end

		if options.keys.include?(:maximum)
			max = options[:maximum]
			test "#{brand}should require max length of #{max} for #{attr}" do
#	because the model may have other requirements
#	just check to ensure that we don't get a :too_long error
#						assert_difference "#{model}.count" do
					value = 'x'*(max)
					object = create_object(attr.to_sym => value)
					assert_equal max, object.send(attr.to_sym).length
					assert_equal object.send(attr.to_sym), value
					assert !object.errors.on_attr_and_type(attr.to_sym, :too_long)
#						end
				assert_no_difference "#{model}.count" do
					value = 'x'*(max+1)
					object = create_object(attr.to_sym => value)
					assert_equal max+1, object.send(attr.to_sym).length
					assert_equal object.send(attr.to_sym), value
					assert object.errors.on_attr_and_type(attr.to_sym, :too_long)
				end
			end
		end

	end
end

#assert_should_require_attribute_not_nil(*attributes) ⇒ Object Also known as: assert_should_require_attributes_not_nil, assert_should_require_not_nil



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/simply_testable/attributes.rb', line 44

def assert_should_require_attribute_not_nil(*attributes)
	options = attributes.extract_options!
	model = options[:model] || st_model_name
	
	attributes.each do |attr|
		attr = attr.to_s
		test "#{brand}should require #{attr} not nil" do
			assert_no_difference "#{model}.count" do
				object = create_object(attr.to_sym => nil)
				assert object.errors.on(attr.to_sym)
			end
		end
	end
end

#assert_should_require_unique_attribute(*attributes) ⇒ Object Also known as: assert_should_require_unique_attributes, assert_should_require_unique



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
# File 'lib/simply_testable/attributes.rb', line 10

def assert_should_require_unique_attribute(*attributes)
	options = attributes.extract_options!
	model = options[:model] || st_model_name
	
	attributes.each do |attr|
		attr = attr.to_s
		title = "#{brand}should require unique #{attr}"
		scope = options[:scope]
		unless scope.blank?
			title << " scope "
			title << (( scope.is_a?(Array) )?scope.join(','):scope.to_s)
		end
		test title do
			o = create_object
			assert_no_difference "#{model}.count" do
				attrs = { attr.to_sym => o.send(attr) }
				if( scope.is_a?(String) || scope.is_a?(Symbol) )
					attrs[scope.to_sym] = o.send(scope.to_sym)
				elsif scope.is_a?(Array)
					scope.each do |s|
						attrs[s.to_sym] = o.send(s.to_sym)
					end
				end 
				object = create_object(attrs)
				assert object.errors.on_attr_and_type(attr.to_sym, :taken)
			end
		end
	end
end