Module: CommonLib::ActiveSupportExtension::Assertions::ClassMethods

Defined in:
lib/common_lib/active_support_extension/assertions.rb

Instance Method Summary collapse

Instance Method Details

#assert_should_accept_only_good_values(*attributes) ⇒ Object

def assert_should_behave_like_a_hash(*args)



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
# File 'lib/common_lib/active_support_extension/assertions.rb', line 59

def assert_should_accept_only_good_values(*attributes)
	options = {
		:good_values => [],
		:bad_values  => []
	}
	options.update(attributes.extract_options!)
	model = options[:model] || model_name_without_test

	attributes.flatten.each do |field|

		[options[:bad_values]].flatten.each do |value|

#	could be a naming problem if both nil and blank are passed

			test "should NOT allow #{value||'nil'} for #{field}" do
#						object = model.constantize.new(field => value)
#	what if field is protected?
				object = model.constantize.new
				object.send("#{field}=", value)
				assert_equal object.send(field), value
				object.valid?
				assert object.errors.matching?(field,'is not included in the list')
			end

		end

		[options[:good_values]].flatten.each do |value|

#	could be a naming problem if both nil and blank are passed

			test "should allow #{value||'nil'} for #{field}" do
#						object = model.constantize.new(field => value)
#	what if field is protected?
				object = model.constantize.new
				object.send("#{field}=", value)
				assert_equal object.send(field), value
				object.valid?
				assert !object.errors.include?(field)
			end

		end

	end	#	attributes.flatten.each do |field|

end

#assert_should_behave_like_a_hash(*args) ⇒ Object



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
# File 'lib/common_lib/active_support_extension/assertions.rb', line 28

def assert_should_behave_like_a_hash(*args)
	options = {
		:key => :key,
		:value => :description
	}
	options.update(args.extract_options!)
	model = options[:model] || model_name_without_test

	assert_should_require_attribute( options[:key], options[:value] )
	assert_should_require_unique_attribute( options[:key], options[:value] )
	assert_should_require_attribute_length( options[:key], options[:value],
		:maximum => 250 )

	test "should find by key with ['string']" do
		object = create_object
		assert object.is_a?(model.constantize)
		found = (model.constantize)[object.key.to_s]
		assert found.is_a?(model.constantize)
		assert_equal object, found
	end

	test "should find by key with [:symbol]" do
		object = create_object
		assert object.is_a?(model.constantize)
		found = (model.constantize)[object.key.to_sym]
		assert found.is_a?(model.constantize)
		assert_equal object, found
	end

end

#assert_should_create_default_object(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/common_lib/active_support_extension/assertions.rb', line 14

def assert_should_create_default_object(*args)
	options = {}
	options.update(args.extract_options!)
	model = options[:model] || model_name_without_test

	test "should create default #{model.underscore}" do
		assert_difference( "#{model}.count", 1 ) do
			object = create_object
			assert !object.new_record?, 
				"#{object.errors.full_messages.to_sentence}"
		end
	end
end