iGEL-shoulda 2.10.2

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.
Files changed (168) hide show
  1. data/CONTRIBUTION_GUIDELINES.rdoc +10 -0
  2. data/MIT-LICENSE +22 -0
  3. data/README.rdoc +171 -0
  4. data/Rakefile +72 -0
  5. data/bin/convert_to_should_syntax +42 -0
  6. data/lib/shoulda.rb +9 -0
  7. data/lib/shoulda/action_controller.rb +26 -0
  8. data/lib/shoulda/action_controller/macros.rb +240 -0
  9. data/lib/shoulda/action_controller/matchers.rb +37 -0
  10. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +109 -0
  11. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +57 -0
  12. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +81 -0
  13. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +74 -0
  14. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +81 -0
  15. data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
  16. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +87 -0
  17. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +85 -0
  18. data/lib/shoulda/action_mailer.rb +10 -0
  19. data/lib/shoulda/action_mailer/assertions.rb +38 -0
  20. data/lib/shoulda/action_view.rb +10 -0
  21. data/lib/shoulda/action_view/macros.rb +61 -0
  22. data/lib/shoulda/active_record.rb +16 -0
  23. data/lib/shoulda/active_record/assertions.rb +69 -0
  24. data/lib/shoulda/active_record/helpers.rb +27 -0
  25. data/lib/shoulda/active_record/macros.rb +512 -0
  26. data/lib/shoulda/active_record/matchers.rb +43 -0
  27. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  28. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
  29. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  30. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  31. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  32. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  33. data/lib/shoulda/active_record/matchers/have_db_index_matcher.rb +112 -0
  34. data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +128 -0
  35. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
  36. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
  37. data/lib/shoulda/active_record/matchers/validate_format_of_matcher.rb +67 -0
  38. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
  39. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  40. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  41. data/lib/shoulda/active_record/matchers/validation_matcher.rb +57 -0
  42. data/lib/shoulda/assertions.rb +71 -0
  43. data/lib/shoulda/autoload_macros.rb +46 -0
  44. data/lib/shoulda/context.rb +402 -0
  45. data/lib/shoulda/helpers.rb +8 -0
  46. data/lib/shoulda/macros.rb +133 -0
  47. data/lib/shoulda/private_helpers.rb +13 -0
  48. data/lib/shoulda/proc_extensions.rb +14 -0
  49. data/lib/shoulda/rails.rb +13 -0
  50. data/lib/shoulda/rspec.rb +11 -0
  51. data/lib/shoulda/tasks.rb +3 -0
  52. data/lib/shoulda/tasks/list_tests.rake +29 -0
  53. data/lib/shoulda/tasks/yaml_to_shoulda.rake +28 -0
  54. data/lib/shoulda/test_unit.rb +22 -0
  55. data/rails/init.rb +7 -0
  56. data/test/README +36 -0
  57. data/test/fail_macros.rb +39 -0
  58. data/test/fixtures/addresses.yml +3 -0
  59. data/test/fixtures/friendships.yml +0 -0
  60. data/test/fixtures/posts.yml +5 -0
  61. data/test/fixtures/products.yml +0 -0
  62. data/test/fixtures/taggings.yml +0 -0
  63. data/test/fixtures/tags.yml +9 -0
  64. data/test/fixtures/users.yml +6 -0
  65. data/test/functional/posts_controller_test.rb +124 -0
  66. data/test/functional/users_controller_test.rb +19 -0
  67. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +68 -0
  68. data/test/matchers/active_record/allow_value_matcher_test.rb +64 -0
  69. data/test/matchers/active_record/association_matcher_test.rb +263 -0
  70. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +80 -0
  71. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +158 -0
  72. data/test/matchers/active_record/have_db_column_matcher_test.rb +169 -0
  73. data/test/matchers/active_record/have_db_index_matcher_test.rb +91 -0
  74. data/test/matchers/active_record/have_named_scope_matcher_test.rb +65 -0
  75. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +29 -0
  76. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +44 -0
  77. data/test/matchers/active_record/validate_format_of_matcher_test.rb +39 -0
  78. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +52 -0
  79. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +157 -0
  80. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +147 -0
  81. data/test/matchers/controller/assign_to_matcher_test.rb +38 -0
  82. data/test/matchers/controller/filter_param_matcher_test.rb +32 -0
  83. data/test/matchers/controller/render_with_layout_matcher_test.rb +33 -0
  84. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +32 -0
  85. data/test/matchers/controller/respond_with_matcher_test.rb +106 -0
  86. data/test/matchers/controller/route_matcher_test.rb +58 -0
  87. data/test/matchers/controller/set_session_matcher_test.rb +38 -0
  88. data/test/matchers/controller/set_the_flash_matcher.rb +41 -0
  89. data/test/model_builder.rb +106 -0
  90. data/test/other/autoload_macro_test.rb +18 -0
  91. data/test/other/context_test.rb +189 -0
  92. data/test/other/convert_to_should_syntax_test.rb +63 -0
  93. data/test/other/helpers_test.rb +340 -0
  94. data/test/other/private_helpers_test.rb +32 -0
  95. data/test/other/should_test.rb +271 -0
  96. data/test/rails_root/app/controllers/application_controller.rb +25 -0
  97. data/test/rails_root/app/controllers/posts_controller.rb +87 -0
  98. data/test/rails_root/app/controllers/users_controller.rb +84 -0
  99. data/test/rails_root/app/helpers/application_helper.rb +3 -0
  100. data/test/rails_root/app/helpers/posts_helper.rb +2 -0
  101. data/test/rails_root/app/helpers/users_helper.rb +2 -0
  102. data/test/rails_root/app/models/address.rb +7 -0
  103. data/test/rails_root/app/models/flea.rb +3 -0
  104. data/test/rails_root/app/models/friendship.rb +4 -0
  105. data/test/rails_root/app/models/pets/cat.rb +7 -0
  106. data/test/rails_root/app/models/pets/dog.rb +10 -0
  107. data/test/rails_root/app/models/post.rb +12 -0
  108. data/test/rails_root/app/models/product.rb +12 -0
  109. data/test/rails_root/app/models/profile.rb +2 -0
  110. data/test/rails_root/app/models/registration.rb +2 -0
  111. data/test/rails_root/app/models/tag.rb +8 -0
  112. data/test/rails_root/app/models/tagging.rb +4 -0
  113. data/test/rails_root/app/models/treat.rb +3 -0
  114. data/test/rails_root/app/models/user.rb +32 -0
  115. data/test/rails_root/app/views/layouts/posts.rhtml +19 -0
  116. data/test/rails_root/app/views/layouts/users.rhtml +17 -0
  117. data/test/rails_root/app/views/layouts/wide.html.erb +1 -0
  118. data/test/rails_root/app/views/posts/edit.rhtml +27 -0
  119. data/test/rails_root/app/views/posts/index.rhtml +25 -0
  120. data/test/rails_root/app/views/posts/new.rhtml +26 -0
  121. data/test/rails_root/app/views/posts/show.rhtml +18 -0
  122. data/test/rails_root/app/views/users/edit.rhtml +22 -0
  123. data/test/rails_root/app/views/users/index.rhtml +22 -0
  124. data/test/rails_root/app/views/users/new.rhtml +21 -0
  125. data/test/rails_root/app/views/users/show.rhtml +13 -0
  126. data/test/rails_root/config/boot.rb +110 -0
  127. data/test/rails_root/config/database.yml +4 -0
  128. data/test/rails_root/config/environment.rb +18 -0
  129. data/test/rails_root/config/environments/test.rb +0 -0
  130. data/test/rails_root/config/initializers/new_rails_defaults.rb +15 -0
  131. data/test/rails_root/config/initializers/shoulda.rb +8 -0
  132. data/test/rails_root/config/routes.rb +6 -0
  133. data/test/rails_root/db/migrate/001_create_users.rb +19 -0
  134. data/test/rails_root/db/migrate/002_create_posts.rb +13 -0
  135. data/test/rails_root/db/migrate/003_create_taggings.rb +12 -0
  136. data/test/rails_root/db/migrate/004_create_tags.rb +11 -0
  137. data/test/rails_root/db/migrate/005_create_dogs.rb +12 -0
  138. data/test/rails_root/db/migrate/006_create_addresses.rb +14 -0
  139. data/test/rails_root/db/migrate/007_create_fleas.rb +11 -0
  140. data/test/rails_root/db/migrate/008_create_dogs_fleas.rb +12 -0
  141. data/test/rails_root/db/migrate/009_create_products.rb +17 -0
  142. data/test/rails_root/db/migrate/010_create_friendships.rb +14 -0
  143. data/test/rails_root/db/migrate/011_create_treats.rb +12 -0
  144. data/test/rails_root/db/migrate/20090506203502_create_profiles.rb +12 -0
  145. data/test/rails_root/db/migrate/20090506203536_create_registrations.rb +14 -0
  146. data/test/rails_root/db/migrate/20090513104502_create_cats.rb +12 -0
  147. data/test/rails_root/db/schema.rb +0 -0
  148. data/test/rails_root/public/404.html +30 -0
  149. data/test/rails_root/public/422.html +30 -0
  150. data/test/rails_root/public/500.html +30 -0
  151. data/test/rails_root/script/console +3 -0
  152. data/test/rails_root/script/generate +3 -0
  153. data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
  154. data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  155. data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  156. data/test/rspec_test.rb +207 -0
  157. data/test/test_helper.rb +28 -0
  158. data/test/unit/address_test.rb +10 -0
  159. data/test/unit/cat_test.rb +7 -0
  160. data/test/unit/dog_test.rb +9 -0
  161. data/test/unit/flea_test.rb +6 -0
  162. data/test/unit/friendship_test.rb +6 -0
  163. data/test/unit/post_test.rb +19 -0
  164. data/test/unit/product_test.rb +23 -0
  165. data/test/unit/tag_test.rb +15 -0
  166. data/test/unit/tagging_test.rb +6 -0
  167. data/test/unit/user_test.rb +80 -0
  168. metadata +264 -0
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require 'users_controller'
3
+
4
+ # Re-raise errors caught by the controller.
5
+ class UsersController; def rescue_action(e) raise e end; end
6
+
7
+ class UsersControllerTest < ActionController::TestCase
8
+ fixtures :all
9
+
10
+ def setup
11
+ @controller = UsersController.new
12
+ @request = ActionController::TestRequest.new
13
+ @response = ActionController::TestResponse.new
14
+ @user = User.find(:first)
15
+ end
16
+
17
+ should_filter_params :ssn
18
+
19
+ end
@@ -0,0 +1,68 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class AllowMassAssignmentOfMatcherTest < ActiveSupport::TestCase # :nodoc:
4
+
5
+ context "an attribute that is blacklisted from mass-assignment" do
6
+ setup do
7
+ define_model :example, :attr => :string do
8
+ attr_protected :attr
9
+ end
10
+ @model = Example.new
11
+ end
12
+
13
+ should "reject being mass-assignable" do
14
+ assert_rejects allow_mass_assignment_of(:attr), @model
15
+ end
16
+ end
17
+
18
+ context "an attribute that is not whitelisted for mass-assignment" do
19
+ setup do
20
+ define_model :example, :attr => :string, :other => :string do
21
+ attr_accessible :other
22
+ end
23
+ @model = Example.new
24
+ end
25
+
26
+ should "reject being mass-assignable" do
27
+ assert_rejects allow_mass_assignment_of(:attr), @model
28
+ end
29
+ end
30
+
31
+ context "an attribute that is whitelisted for mass-assignment" do
32
+ setup do
33
+ define_model :example, :attr => :string do
34
+ attr_accessible :attr
35
+ end
36
+ @model = Example.new
37
+ end
38
+
39
+ should "accept being mass-assignable" do
40
+ assert_accepts allow_mass_assignment_of(:attr), @model
41
+ end
42
+ end
43
+
44
+ context "an attribute not included in the mass-assignment blacklist" do
45
+ setup do
46
+ define_model :example, :attr => :string, :other => :string do
47
+ attr_protected :other
48
+ end
49
+ @model = Example.new
50
+ end
51
+
52
+ should "accept being mass-assignable" do
53
+ assert_accepts allow_mass_assignment_of(:attr), @model
54
+ end
55
+ end
56
+
57
+ context "an attribute on a class with no protected attributes" do
58
+ setup do
59
+ define_model :example, :attr => :string
60
+ @model = Example.new
61
+ end
62
+
63
+ should "accept being mass-assignable" do
64
+ assert_accepts allow_mass_assignment_of(:attr), @model
65
+ end
66
+ end
67
+
68
+ end
@@ -0,0 +1,64 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class AllowValueMatcherTest < ActiveSupport::TestCase # :nodoc:
4
+
5
+ context "an attribute with a format validation" do
6
+ setup do
7
+ define_model :example, :attr => :string do
8
+ validates_format_of :attr, :with => /abc/
9
+ end
10
+ @model = Example.new
11
+ end
12
+
13
+ should "allow a good value" do
14
+ assert_accepts allow_value("abcde").for(:attr), @model
15
+ end
16
+
17
+ should "not allow a bad value" do
18
+ assert_rejects allow_value("xyz").for(:attr), @model
19
+ end
20
+ end
21
+
22
+ context "an attribute with a format validation and a custom message" do
23
+ setup do
24
+ define_model :example, :attr => :string do
25
+ validates_format_of :attr, :with => /abc/, :message => 'bad value'
26
+ end
27
+ @model = Example.new
28
+ end
29
+
30
+ should "allow a good value" do
31
+ assert_accepts allow_value('abcde').for(:attr).with_message(/bad/),
32
+ @model
33
+ end
34
+
35
+ should "not allow a bad value" do
36
+ assert_rejects allow_value('xyz').for(:attr).with_message(/bad/),
37
+ @model
38
+ end
39
+ end
40
+
41
+ context "an attribute with several validations" do
42
+ setup do
43
+ define_model :example, :attr => :string do
44
+ validates_presence_of :attr
45
+ validates_length_of :attr, :within => 1..5
46
+ validates_numericality_of :attr, :greater_than_or_equal_to => 1,
47
+ :less_than_or_equal_to => 50000
48
+ end
49
+ @model = Example.new
50
+ end
51
+
52
+ should "allow a good value" do
53
+ assert_accepts allow_value("12345").for(:attr), @model
54
+ end
55
+
56
+ bad_values = [nil, "", "abc", "0", "50001", "123456"]
57
+ bad_values.each do |value|
58
+ should "not allow a bad value (#{value.inspect})" do
59
+ assert_rejects allow_value(value).for(:attr), @model
60
+ end
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,263 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class AssociationMatcherTest < ActiveSupport::TestCase # :nodoc:
4
+
5
+ context "belong_to" do
6
+ setup do
7
+ @matcher = belong_to(:parent)
8
+ end
9
+
10
+ should "accept a good association with the default foreign key" do
11
+ define_model :parent
12
+ define_model :child, :parent_id => :integer do
13
+ belongs_to :parent
14
+ end
15
+ assert_accepts @matcher, Child.new
16
+ end
17
+
18
+ should "reject a nonexistent association" do
19
+ define_model :child
20
+ assert_rejects @matcher, Child.new
21
+ end
22
+
23
+ should "reject an association of the wrong type" do
24
+ define_model :parent, :child_id => :integer
25
+ child_class = define_model :child do
26
+ has_one :parent
27
+ end
28
+ assert_rejects @matcher, Child.new
29
+ end
30
+
31
+ should "reject an association that has a nonexistent foreign key" do
32
+ define_model :parent
33
+ define_model :child do
34
+ belongs_to :parent
35
+ end
36
+ assert_rejects @matcher, Child.new
37
+ end
38
+
39
+ should "accept an association with an existing custom foreign key" do
40
+ define_model :parent
41
+ define_model :child, :guardian_id => :integer do
42
+ belongs_to :parent, :foreign_key => 'guardian_id'
43
+ end
44
+ assert_accepts @matcher, Child.new
45
+ end
46
+
47
+ should "accept a polymorphic association" do
48
+ define_model :child, :parent_type => :string,
49
+ :parent_id => :integer do
50
+ belongs_to :parent, :polymorphic => true
51
+ end
52
+ assert_accepts @matcher, Child.new
53
+ end
54
+
55
+ should "accept an association with a valid :dependent option" do
56
+ define_model :parent
57
+ define_model :child, :parent_id => :integer do
58
+ belongs_to :parent, :dependent => :destroy
59
+ end
60
+ assert_accepts @matcher.dependent(:destroy), Child.new
61
+ end
62
+
63
+ should "reject an association with a bad :dependent option" do
64
+ define_model :parent
65
+ define_model :child, :parent_id => :integer do
66
+ belongs_to :parent
67
+ end
68
+ assert_rejects @matcher.dependent(:destroy), Child.new
69
+ end
70
+ end
71
+
72
+ context "have_many" do
73
+ setup do
74
+ @matcher = have_many(:children)
75
+ end
76
+
77
+ should "accept a valid association without any options" do
78
+ define_model :child, :parent_id => :integer
79
+ define_model :parent do
80
+ has_many :children
81
+ end
82
+ assert_accepts @matcher, Parent.new
83
+ end
84
+
85
+ should "accept a valid association with a :through option" do
86
+ define_model :child
87
+ define_model :conception, :child_id => :integer,
88
+ :parent_id => :integer do
89
+ belongs_to :child
90
+ end
91
+ define_model :parent do
92
+ has_many :conceptions
93
+ has_many :children, :through => :conceptions
94
+ end
95
+ assert_accepts @matcher, Parent.new
96
+ end
97
+
98
+ should "accept a valid association with an :as option" do
99
+ define_model :child, :guardian_type => :string,
100
+ :guardian_id => :integer
101
+ define_model :parent do
102
+ has_many :children, :as => :guardian
103
+ end
104
+ assert_accepts @matcher, Parent.new
105
+ end
106
+
107
+ should "reject an association that has a nonexistent foreign key" do
108
+ define_model :child
109
+ define_model :parent do
110
+ has_many :children
111
+ end
112
+ assert_rejects @matcher, Parent.new
113
+ end
114
+
115
+ should "reject an association with a bad :as option" do
116
+ define_model :child, :caretaker_type => :string,
117
+ :caretaker_id => :integer
118
+ define_model :parent do
119
+ has_many :children, :as => :guardian
120
+ end
121
+ assert_rejects @matcher, Parent.new
122
+ end
123
+
124
+ should "reject an association that has a bad :through option" do
125
+ define_model :child, :parent_id => :integer
126
+ define_model :parent do
127
+ has_many :children
128
+ end
129
+ assert_rejects @matcher.through(:conceptions),
130
+ Parent.new,
131
+ :message => /does not have any relationship to conceptions/
132
+ end
133
+
134
+ should "reject an association that has the wrong :through option" do
135
+ define_model :child
136
+ define_model :conception, :child_id => :integer,
137
+ :parent_id => :integer do
138
+ belongs_to :child
139
+ end
140
+ define_model :parent do
141
+ has_many :conceptions
142
+ has_many :relationships
143
+ has_many :children, :through => :conceptions
144
+ end
145
+ assert_rejects @matcher.through(:relationships),
146
+ Parent.new,
147
+ :message => /through relationships, but got it through conceptions/
148
+ end
149
+
150
+ should "accept an association with a valid :dependent option" do
151
+ define_model :child, :parent_id => :integer
152
+ define_model :parent do
153
+ has_many :children, :dependent => :destroy
154
+ end
155
+ assert_accepts @matcher.dependent(:destroy), Parent.new
156
+ end
157
+
158
+ should "reject an association with a bad :dependent option" do
159
+ define_model :child, :parent_id => :integer
160
+ define_model :parent do
161
+ has_many :children
162
+ end
163
+ assert_rejects @matcher.dependent(:destroy), Parent.new
164
+ end
165
+ end
166
+
167
+ context "have_one" do
168
+ setup do
169
+ @matcher = have_one(:profile)
170
+ end
171
+
172
+ should "accept a valid association without any options" do
173
+ define_model :profile, :person_id => :integer
174
+ define_model :person do
175
+ has_one :profile
176
+ end
177
+ assert_accepts @matcher, Person.new
178
+ end
179
+
180
+ should "accept a valid association with an :as option" do
181
+ define_model :profile, :profilable_id => :integer,
182
+ :profilable_type => :string
183
+ define_model :person do
184
+ has_one :profile, :as => :profilable
185
+ end
186
+ assert_accepts @matcher, Person.new
187
+ end
188
+
189
+ should "reject an association that has a nonexistent foreign key" do
190
+ define_model :profile
191
+ define_model :person do
192
+ has_one :profile
193
+ end
194
+ assert_rejects @matcher, Person.new
195
+ end
196
+
197
+ should "reject an association with a bad :as option" do
198
+ define_model :profile, :profilable_id => :integer,
199
+ :profilable_type => :string
200
+ define_model :person do
201
+ has_one :profile, :as => :describable
202
+ end
203
+ assert_rejects @matcher, Person.new
204
+ end
205
+
206
+ should "accept an association with a valid :dependent option" do
207
+ define_model :profile, :person_id => :integer
208
+ define_model :person do
209
+ has_one :profile, :dependent => :destroy
210
+ end
211
+ assert_accepts @matcher.dependent(:destroy), Person.new
212
+ end
213
+
214
+ should "reject an association with a bad :dependent option" do
215
+ define_model :profile, :person_id => :integer
216
+ define_model :person do
217
+ has_one :profile
218
+ end
219
+ assert_rejects @matcher.dependent(:destroy), Person.new
220
+ end
221
+ end
222
+
223
+ context "have_and_belong_to_many" do
224
+ setup do
225
+ @matcher = have_and_belong_to_many(:relatives)
226
+ end
227
+
228
+ should "accept a valid association" do
229
+ define_model :relatives
230
+ define_model :person do
231
+ has_and_belongs_to_many :relatives
232
+ end
233
+ define_model :people_relative, :person_id => :integer,
234
+ :relative_id => :integer
235
+ assert_accepts @matcher, Person.new
236
+ end
237
+
238
+ should "reject a nonexistent association" do
239
+ define_model :relatives
240
+ define_model :person
241
+ define_model :people_relative, :person_id => :integer,
242
+ :relative_id => :integer
243
+ assert_rejects @matcher, Person.new
244
+ end
245
+
246
+ should "reject an association with a nonexistent join table" do
247
+ define_model :relatives
248
+ define_model :person do
249
+ has_and_belongs_to_many :relatives
250
+ end
251
+ assert_rejects @matcher, Person.new
252
+ end
253
+
254
+ should "reject an association of the wrong type" do
255
+ define_model :relatives, :person_id => :integer
256
+ define_model :person do
257
+ has_many :relatives
258
+ end
259
+ assert_rejects @matcher, Person.new
260
+ end
261
+ end
262
+
263
+ end
@@ -0,0 +1,80 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class EnsureInclusionOfMatcherTest < ActiveSupport::TestCase # :nodoc:
4
+
5
+ context "an attribute which must be included in a range" do
6
+ setup do
7
+ @model = define_model(:example, :attr => :integer) do
8
+ validates_inclusion_of :attr, :in => 2..5
9
+ end.new
10
+ end
11
+
12
+ should "accept ensuring the correct range" do
13
+ assert_accepts ensure_inclusion_of(:attr).in_range(2..5), @model
14
+ end
15
+
16
+ should "reject ensuring a lower minimum value" do
17
+ assert_rejects ensure_inclusion_of(:attr).in_range(1..5), @model
18
+ end
19
+
20
+ should "reject ensuring a higher minimum value" do
21
+ assert_rejects ensure_inclusion_of(:attr).in_range(3..5), @model
22
+ end
23
+
24
+ should "reject ensuring a lower maximum value" do
25
+ assert_rejects ensure_inclusion_of(:attr).in_range(2..4), @model
26
+ end
27
+
28
+ should "reject ensuring a higher maximum value" do
29
+ assert_rejects ensure_inclusion_of(:attr).in_range(2..6), @model
30
+ end
31
+
32
+ should "not override the default message with a blank" do
33
+ assert_accepts ensure_inclusion_of(:attr).
34
+ in_range(2..5).
35
+ with_message(nil),
36
+ @model
37
+ end
38
+ end
39
+
40
+ context "an attribute with a custom ranged value validation" do
41
+ setup do
42
+ @model = define_model(:example, :attr => :string) do
43
+ validates_inclusion_of :attr, :in => 2..4, :message => 'not good'
44
+
45
+ end.new
46
+ end
47
+
48
+ should "accept ensuring the correct range" do
49
+ assert_accepts ensure_inclusion_of(:attr).
50
+ in_range(2..4).
51
+ with_message(/not good/),
52
+ @model
53
+ end
54
+ end
55
+
56
+ context "an attribute with custom range validations" do
57
+ setup do
58
+ define_model :example, :attr => :integer do
59
+ def validate
60
+ if attr < 2
61
+ errors.add(:attr, 'too low')
62
+ elsif attr > 5
63
+ errors.add(:attr, 'too high')
64
+ end
65
+ end
66
+ end
67
+ @model = Example.new
68
+ end
69
+
70
+ should "accept ensuring the correct range and messages" do
71
+ assert_accepts ensure_inclusion_of(:attr).
72
+ in_range(2..5).
73
+ with_low_message(/low/).
74
+ with_high_message(/high/),
75
+ @model
76
+ end
77
+
78
+ end
79
+
80
+ end