evelpidon_test_helpers 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Evelpidon Test Helpers changelog
2
2
 
3
+ ## 0.2.0 / 2011-10-18
4
+
5
+ * Modulirization of ActiveModel helpers.
6
+ * "Default model values" test helpers.
7
+
3
8
  ## 0.1.1 / 2011-10-13
4
9
 
5
10
  * (Doc) Better README
@@ -1,120 +1,12 @@
1
1
  module EvelpidonTestHelpers
2
- # +ActiveModel+ related helpers
2
+ # +ActiveModel+ related helpers. See sub-modules for available helpers.
3
3
  module ActiveModel
4
- # Asserts that the given ++ActiveModel++ is "valid".
5
- # If not, the error message is the full error messages.
6
- def assert_valid(object, additional_message = nil)
7
- is_valid = object.valid?
8
- error_message = additional_message ?
9
- "#{additional_message}\n\nErrors:\n\n#{object.errors.full_messages.join("\n")}\n\n" :
10
- "Errors:\n#{object.errors.full_messages.join("\n")}\n\n"
11
- assert is_valid, error_message
12
- end
13
-
14
- # Asserts that the given +attribute+ on the given +ActiveModel+ contains no errors.
15
- # If not, the error message is the joint string of the errors on this +attribute+.
16
- def assert_valid_attribute(object, attribute, message = "")
17
- object.valid?
18
- errors_on_attribute = object.errors[attribute].length
19
- error_messages = object.errors[attribute].join(',')
20
- message << "\nExpected zero errors on #{attribute} but got #{errors_on_attribute} :\n#{error_messages}"
21
- assert_equal 0, errors_on_attribute, message
22
- end
23
-
24
- # Asserts that the given ++ActiveModel++ is "invalid".
25
- # The ++attributes_with_errors++ options should a hash of attributes to be specifically
26
- # examined for having errors. For example : {:email => 1, :username => 2} (etc).
27
- def assert_invalid(object, attributes_with_errors = {})
28
- assert object.invalid?, "Expected #{object} to be invalid, but was actually valid"
29
-
30
- attributes_with_errors.each do |attribute, expected_number_of_errors|
31
- actual_errors_on_attribute = object.errors[attribute].length
32
- error_message = "Expected #{expected_number_of_errors} errors on #{attribute}, but were actually #{actual_errors_on_attribute} : \n"
33
- error_message << "#{object.errors[attribute].join("\n")}"
34
- assert_equal expected_number_of_errors, actual_errors_on_attribute, error_message
35
- end
36
- end
37
-
38
- # Asserts that the given +attribute+ on the given +ActiveModel+ contains at least one error.
39
- def assert_invalid_attribute(object, attribute, message = "")
40
- object.valid?
41
- errors_on_attribute = object.errors[attribute].length
42
- error_messages = object.errors[attribute].join(',')
43
- message << "\nExpected at least one error on #{attribute} but got #{errors_on_attribute} :\n#{error_messages}"
44
- assert errors_on_attribute > 0, message
45
- end
46
-
47
- # Asserts that the given +attributes+ result to a valid +ActiveModel+ instance.
48
- #
49
- # This helper chooses the model name based on the current test name, so for example
50
- # when the test that is running is named FooTest it will try to instantiate a new Foo
51
- # model, update the attributes (by-passing mass assignment protection) and then call
52
- # +assert_valid+ on it.
53
- def assert_valid_model_attributes(attributes)
54
- model = self.class.to_s.gsub("Controller", "").gsub("Test", "").constantize
55
- instance = model.new
56
-
57
- attributes.each do |attribute_name, attribute_value|
58
- instance.send("#{attribute_name}=", attribute_value)
59
- end
60
-
61
- assert_valid instance, instance.inspect
62
- end
63
-
64
- # Asserts that all the records for the current model under test are valid.
65
- #
66
- # This helper chooses the model name based on the current test name, so for example
67
- # when the test that is running is named FooTest it will try to load all the records of Foo
68
- # through Foo.all and then call +assert_valid+ on each one of them.
69
- def assert_valid_fixtures
70
- model = self.class.to_s.gsub("Controller", "").gsub("Test", "").constantize
71
- model.all.each do |fixture|
72
- assert_valid fixture, fixture.inspect
73
- end
74
- end
75
-
76
- # Asserts that the given +attribute+ on the given +model+ cannot be assigned through
77
- # mass-assignment (like +update_attributes).
78
- #
79
- # @param model [ActiveModel] A properly initialized instance of the class that we want to test.
80
- # @param attribute [Symbol] The attribute that is protected
81
- # @param value [Object] An optional value to use when trying to assign the attribute.
82
- def assert_attribute_protected(model, attribute, value = "foo")
83
- model.send("#{attribute}=", nil)
84
- model.attributes = {attribute => value}
85
- assert_nil model.send(attribute), "#{attribute} could be set through 'update_attributes' call"
86
-
87
- model.send("#{attribute}=", value)
88
- assert_equal value, model.send(attribute)
89
- end
90
-
91
- # Asserts that the given +observer+ class will receive the +notification+.
92
- #
93
- # @param observer [Class] the observer class
94
- # @param notification [Symbol] the notification
95
- # @param options [Hash] extra options :
96
- # * :object [Object, Mocha::ParameterMatchers::Base] the object that should be passed to the observer.
97
- # * :times [Integer] the number of times that the notification should be sent to the observer.
98
- #
99
- def assert_observer_notified(observer, notification, options = {})
100
- options.reverse_merge!(:times => 1, :object => anything)
101
- observer.instance.expects(notification).with(options[:object]).times(options[:times])
102
- end
103
-
104
- # Asserts that the given +observer+ class will never receive the +notification+.
105
- #
106
- # @param observer [Class] the observer class
107
- # @param notification [Symbol] the notification
108
- # @param options [Hash] extra options
109
- #
110
- def assert_observer_not_notified(observer, notification, options = {})
111
- observer.instance.expects(notification).never
112
- end
113
4
  end
114
5
  end
115
6
 
116
- module ActiveSupport
117
- class TestCase
118
- include EvelpidonTestHelpers::ActiveModel
119
- end
120
- end if defined?(ActiveModel)
7
+ require "evelpidon_test_helpers/active_model/model_validations"
8
+ require "evelpidon_test_helpers/active_model/attribute_validations"
9
+ require "evelpidon_test_helpers/active_model/fixtures"
10
+ require "evelpidon_test_helpers/active_model/mass_assignment"
11
+ require "evelpidon_test_helpers/active_model/default_model_values"
12
+ require "evelpidon_test_helpers/active_model/observers"
@@ -0,0 +1,32 @@
1
+ module EvelpidonTestHelpers
2
+ module ActiveModel
3
+ module AttributeValidations
4
+ module Assertions
5
+ # Asserts that the given +attribute+ on the given +ActiveModel+ contains no errors.
6
+ # If not, the error message is the joint string of the errors on this +attribute+.
7
+ def assert_valid_attribute(object, attribute, message = "")
8
+ object.valid?
9
+ errors_on_attribute = object.errors[attribute].length
10
+ error_messages = object.errors[attribute].join(',')
11
+ message << "\nExpected zero errors on #{attribute} but got #{errors_on_attribute} :\n#{error_messages}"
12
+ assert_equal 0, errors_on_attribute, message
13
+ end
14
+
15
+ # Asserts that the given +attribute+ on the given +ActiveModel+ contains at least one error.
16
+ def assert_invalid_attribute(object, attribute, message = "")
17
+ object.valid?
18
+ errors_on_attribute = object.errors[attribute].length
19
+ error_messages = object.errors[attribute].join(',')
20
+ message << "\nExpected at least one error on #{attribute} but got #{errors_on_attribute} :\n#{error_messages}"
21
+ assert errors_on_attribute > 0, message
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ module ActiveSupport
29
+ class TestCase
30
+ include EvelpidonTestHelpers::ActiveModel::AttributeValidations::Assertions
31
+ end
32
+ end if defined?(ActiveModel)
@@ -0,0 +1,25 @@
1
+ module EvelpidonTestHelpers
2
+ module ActiveModel
3
+ module DefaultModelValues
4
+ module Assertions
5
+ def assert_default_value(object, attribute, value)
6
+ assert_valid_attribute object, attribute.to_sym
7
+ assert_equal value, object.send(attribute.to_sym)
8
+ end
9
+
10
+ def assert_default_model_value(attribute, value)
11
+ model = self.class.to_s.gsub("Controller", "").gsub("Test", "").constantize
12
+ instance = model.new
13
+
14
+ assert_default_value(instance, attribute, value)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ module ActiveSupport
22
+ class TestCase
23
+ include EvelpidonTestHelpers::ActiveModel::DefaultModelValues::Assertions
24
+ end
25
+ end if defined?(ActiveModel)
@@ -0,0 +1,25 @@
1
+ module EvelpidonTestHelpers
2
+ module ActiveModel
3
+ module Fixtures
4
+ module Assertions
5
+ # Asserts that all the records for the current model under test are valid.
6
+ #
7
+ # This helper chooses the model name based on the current test name, so for example
8
+ # when the test that is running is named FooTest it will try to load all the records of Foo
9
+ # through Foo.all and then call +assert_valid+ on each one of them.
10
+ def assert_valid_fixtures
11
+ model = self.class.to_s.gsub("Controller", "").gsub("Test", "").constantize
12
+ model.all.each do |fixture|
13
+ assert_valid fixture, fixture.inspect
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ module ActiveSupport
22
+ class TestCase
23
+ include EvelpidonTestHelpers::ActiveModel::Fixtures::Assertions
24
+ end
25
+ end if defined?(ActiveModel)
@@ -0,0 +1,28 @@
1
+ module EvelpidonTestHelpers
2
+ module ActiveModel
3
+ module MassAssignment
4
+ module Assertions
5
+ # Asserts that the given +attribute+ on the given +model+ cannot be assigned through
6
+ # mass-assignment (like +update_attributes).
7
+ #
8
+ # @param model [ActiveModel] A properly initialized instance of the class that we want to test.
9
+ # @param attribute [Symbol] The attribute that is protected
10
+ # @param value [Object] An optional value to use when trying to assign the attribute.
11
+ def assert_attribute_protected(model, attribute, value = "foo")
12
+ model.send("#{attribute}=", nil)
13
+ model.attributes = {attribute => value}
14
+ assert_nil model.send(attribute), "#{attribute} could be set through 'update_attributes' call"
15
+
16
+ model.send("#{attribute}=", value)
17
+ assert_equal value, model.send(attribute)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ module ActiveSupport
25
+ class TestCase
26
+ include EvelpidonTestHelpers::ActiveModel::MassAssignment::Assertions
27
+ end
28
+ end if defined?(ActiveModel)
@@ -0,0 +1,54 @@
1
+ module EvelpidonTestHelpers
2
+ module ActiveModel
3
+ module ModelValidations
4
+ module Assertions
5
+ # Asserts that the given ++ActiveModel++ is "valid".
6
+ # If not, the error message is the full error messages.
7
+ def assert_valid(object, additional_message = nil)
8
+ is_valid = object.valid?
9
+ error_message = additional_message ?
10
+ "#{additional_message}\n\nErrors:\n\n#{object.errors.full_messages.join("\n")}\n\n" :
11
+ "Errors:\n#{object.errors.full_messages.join("\n")}\n\n"
12
+ assert is_valid, error_message
13
+ end
14
+
15
+ # Asserts that the given ++ActiveModel++ is "invalid".
16
+ # The ++attributes_with_errors++ options should a hash of attributes to be specifically
17
+ # examined for having errors. For example : {:email => 1, :username => 2} (etc).
18
+ def assert_invalid(object, attributes_with_errors = {})
19
+ assert object.invalid?, "Expected #{object} to be invalid, but was actually valid"
20
+
21
+ attributes_with_errors.each do |attribute, expected_number_of_errors|
22
+ actual_errors_on_attribute = object.errors[attribute].length
23
+ error_message = "Expected #{expected_number_of_errors} errors on #{attribute}, but were actually #{actual_errors_on_attribute} : \n"
24
+ error_message << "#{object.errors[attribute].join("\n")}"
25
+ assert_equal expected_number_of_errors, actual_errors_on_attribute, error_message
26
+ end
27
+ end
28
+
29
+ # Asserts that the given +attributes+ result to a valid +ActiveModel+ instance.
30
+ #
31
+ # This helper chooses the model name based on the current test name, so for example
32
+ # when the test that is running is named FooTest it will try to instantiate a new Foo
33
+ # model, update the attributes (by-passing mass assignment protection) and then call
34
+ # +assert_valid+ on it.
35
+ def assert_valid_model_attributes(attributes)
36
+ model = self.class.to_s.gsub("Controller", "").gsub("Test", "").constantize
37
+ instance = model.new
38
+
39
+ attributes.each do |attribute_name, attribute_value|
40
+ instance.send("#{attribute_name}=", attribute_value)
41
+ end
42
+
43
+ assert_valid instance, instance.inspect
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+ module ActiveSupport
51
+ class TestCase
52
+ include EvelpidonTestHelpers::ActiveModel::ModelValidations::Assertions
53
+ end
54
+ end if defined?(ActiveModel)
@@ -0,0 +1,36 @@
1
+ module EvelpidonTestHelpers
2
+ module ActiveModel
3
+ module Observers
4
+ module Assertions
5
+ # Asserts that the given +observer+ class will receive the +notification+.
6
+ #
7
+ # @param observer [Class] the observer class
8
+ # @param notification [Symbol] the notification
9
+ # @param options [Hash] extra options :
10
+ # * :object [Object, Mocha::ParameterMatchers::Base] the object that should be passed to the observer.
11
+ # * :times [Integer] the number of times that the notification should be sent to the observer.
12
+ #
13
+ def assert_observer_notified(observer, notification, options = {})
14
+ options.reverse_merge!(:times => 1, :object => anything)
15
+ observer.instance.expects(notification).with(options[:object]).times(options[:times])
16
+ end
17
+
18
+ # Asserts that the given +observer+ class will never receive the +notification+.
19
+ #
20
+ # @param observer [Class] the observer class
21
+ # @param notification [Symbol] the notification
22
+ # @param options [Hash] extra options
23
+ #
24
+ def assert_observer_not_notified(observer, notification, options = {})
25
+ observer.instance.expects(notification).never
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ module ActiveSupport
33
+ class TestCase
34
+ include EvelpidonTestHelpers::ActiveModel::Observers::Assertions
35
+ end
36
+ end if defined?(ActiveModel)
@@ -1,3 +1,3 @@
1
1
  module EvelpidonTestHelpers
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evelpidon_test_helpers
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nikos Dimitrakopoulos
@@ -17,7 +17,8 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-10-13 00:00:00 Z
20
+ date: 2011-10-18 00:00:00 +03:00
21
+ default_executable:
21
22
  dependencies:
22
23
  - !ruby/object:Gem::Dependency
23
24
  name: activemodel
@@ -101,9 +102,16 @@ files:
101
102
  - lib/evelpidon_test_helpers.rb
102
103
  - lib/evelpidon_test_helpers/action_controller.rb
103
104
  - lib/evelpidon_test_helpers/active_model.rb
105
+ - lib/evelpidon_test_helpers/active_model/attribute_validations.rb
106
+ - lib/evelpidon_test_helpers/active_model/default_model_values.rb
107
+ - lib/evelpidon_test_helpers/active_model/fixtures.rb
108
+ - lib/evelpidon_test_helpers/active_model/mass_assignment.rb
109
+ - lib/evelpidon_test_helpers/active_model/model_validations.rb
110
+ - lib/evelpidon_test_helpers/active_model/observers.rb
104
111
  - lib/evelpidon_test_helpers/date.rb
105
112
  - lib/evelpidon_test_helpers/sunspot.rb
106
113
  - lib/evelpidon_test_helpers/version.rb
114
+ has_rdoc: true
107
115
  homepage: ""
108
116
  licenses: []
109
117
 
@@ -133,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
141
  requirements: []
134
142
 
135
143
  rubyforge_project: evelpidon_test_helpers
136
- rubygems_version: 1.8.10
144
+ rubygems_version: 1.6.2
137
145
  signing_key:
138
146
  specification_version: 3
139
147
  summary: Various test helpers for Rails projects