technicalpickles-shoulda 2.0.6 → 2.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/README.rdoc +61 -11
  2. data/Rakefile +7 -5
  3. data/lib/shoulda.rb +7 -15
  4. data/lib/shoulda/action_controller.rb +28 -0
  5. data/lib/shoulda/action_controller/helpers.rb +47 -0
  6. data/lib/shoulda/action_controller/macros.rb +277 -0
  7. data/lib/shoulda/action_controller/matchers.rb +37 -0
  8. data/lib/shoulda/action_controller/matchers/assign_to_matcher.rb +109 -0
  9. data/lib/shoulda/action_controller/matchers/filter_param_matcher.rb +57 -0
  10. data/lib/shoulda/action_controller/matchers/render_with_layout_matcher.rb +81 -0
  11. data/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb +70 -0
  12. data/lib/shoulda/action_controller/matchers/respond_with_matcher.rb +81 -0
  13. data/lib/shoulda/action_controller/matchers/route_matcher.rb +93 -0
  14. data/lib/shoulda/action_controller/matchers/set_session_matcher.rb +83 -0
  15. data/lib/shoulda/action_controller/matchers/set_the_flash_matcher.rb +85 -0
  16. data/lib/shoulda/action_mailer.rb +1 -1
  17. data/lib/shoulda/action_mailer/assertions.rb +32 -33
  18. data/lib/shoulda/action_view.rb +10 -0
  19. data/lib/shoulda/action_view/macros.rb +56 -0
  20. data/lib/shoulda/active_record.rb +6 -2
  21. data/lib/shoulda/active_record/assertions.rb +62 -81
  22. data/lib/shoulda/active_record/helpers.rb +40 -0
  23. data/lib/shoulda/active_record/macros.rb +514 -640
  24. data/lib/shoulda/active_record/matchers.rb +42 -0
  25. data/lib/shoulda/active_record/matchers/allow_mass_assignment_of_matcher.rb +83 -0
  26. data/lib/shoulda/active_record/matchers/allow_value_matcher.rb +102 -0
  27. data/lib/shoulda/active_record/matchers/association_matcher.rb +226 -0
  28. data/lib/shoulda/active_record/matchers/ensure_inclusion_of_matcher.rb +87 -0
  29. data/lib/shoulda/active_record/matchers/ensure_length_of_matcher.rb +141 -0
  30. data/lib/shoulda/active_record/matchers/have_db_column_matcher.rb +169 -0
  31. data/lib/shoulda/active_record/matchers/have_index_matcher.rb +105 -0
  32. data/lib/shoulda/active_record/matchers/have_named_scope_matcher.rb +125 -0
  33. data/lib/shoulda/active_record/matchers/have_readonly_attribute_matcher.rb +59 -0
  34. data/lib/shoulda/active_record/matchers/validate_acceptance_of_matcher.rb +41 -0
  35. data/lib/shoulda/active_record/matchers/validate_numericality_of_matcher.rb +39 -0
  36. data/lib/shoulda/active_record/matchers/validate_presence_of_matcher.rb +60 -0
  37. data/lib/shoulda/active_record/matchers/validate_uniqueness_of_matcher.rb +148 -0
  38. data/lib/shoulda/active_record/matchers/validation_matcher.rb +56 -0
  39. data/lib/shoulda/assertions.rb +50 -40
  40. data/lib/shoulda/autoload_macros.rb +46 -0
  41. data/lib/shoulda/context.rb +124 -126
  42. data/lib/shoulda/helpers.rb +5 -7
  43. data/lib/shoulda/macros.rb +63 -64
  44. data/lib/shoulda/private_helpers.rb +16 -18
  45. data/lib/shoulda/rails.rb +5 -11
  46. data/lib/shoulda/rspec.rb +11 -0
  47. data/lib/shoulda/tasks/list_tests.rake +6 -1
  48. data/lib/shoulda/test_unit.rb +19 -0
  49. data/rails/init.rb +7 -1
  50. data/test/README +2 -2
  51. data/test/fail_macros.rb +16 -16
  52. data/test/functional/posts_controller_test.rb +33 -24
  53. data/test/functional/users_controller_test.rb +0 -19
  54. data/test/matchers/active_record/allow_mass_assignment_of_matcher_test.rb +68 -0
  55. data/test/matchers/active_record/allow_value_matcher_test.rb +41 -0
  56. data/test/matchers/active_record/association_matcher_test.rb +258 -0
  57. data/test/matchers/active_record/ensure_inclusion_of_matcher_test.rb +80 -0
  58. data/test/matchers/active_record/ensure_length_of_matcher_test.rb +158 -0
  59. data/test/matchers/active_record/have_db_column_matcher_test.rb +169 -0
  60. data/test/matchers/active_record/have_index_matcher_test.rb +74 -0
  61. data/test/matchers/active_record/have_named_scope_matcher_test.rb +65 -0
  62. data/test/matchers/active_record/have_readonly_attributes_matcher_test.rb +29 -0
  63. data/test/matchers/active_record/validate_acceptance_of_matcher_test.rb +44 -0
  64. data/test/matchers/active_record/validate_numericality_of_matcher_test.rb +52 -0
  65. data/test/matchers/active_record/validate_presence_of_matcher_test.rb +86 -0
  66. data/test/matchers/active_record/validate_uniqueness_of_matcher_test.rb +147 -0
  67. data/test/matchers/controller/assign_to_matcher_test.rb +35 -0
  68. data/test/matchers/controller/filter_param_matcher_test.rb +32 -0
  69. data/test/matchers/controller/render_with_layout_matcher_test.rb +33 -0
  70. data/test/matchers/controller/respond_with_content_type_matcher_test.rb +27 -0
  71. data/test/matchers/controller/respond_with_matcher_test.rb +106 -0
  72. data/test/matchers/controller/route_matcher_test.rb +58 -0
  73. data/test/matchers/controller/set_session_matcher_test.rb +27 -0
  74. data/test/matchers/controller/set_the_flash_matcher.rb +41 -0
  75. data/test/model_builder.rb +106 -0
  76. data/test/other/autoload_macro_test.rb +18 -0
  77. data/test/other/helpers_test.rb +58 -0
  78. data/test/other/private_helpers_test.rb +1 -1
  79. data/test/other/should_test.rb +16 -16
  80. data/test/rails_root/app/controllers/posts_controller.rb +6 -5
  81. data/test/rails_root/app/models/pets/dog.rb +10 -0
  82. data/test/rails_root/app/models/treat.rb +3 -0
  83. data/test/rails_root/app/models/user.rb +4 -3
  84. data/test/rails_root/app/views/layouts/posts.rhtml +2 -0
  85. data/test/rails_root/config/database.yml +1 -1
  86. data/test/rails_root/config/environment.rb +1 -1
  87. data/test/rails_root/config/environments/{sqlite3.rb → test.rb} +0 -0
  88. data/test/rails_root/db/migrate/001_create_users.rb +3 -2
  89. data/test/rails_root/db/migrate/011_create_treats.rb +12 -0
  90. data/test/rails_root/test/shoulda_macros/custom_macro.rb +6 -0
  91. data/test/rails_root/vendor/gems/gem_with_macro-0.0.1/shoulda_macros/gem_macro.rb +6 -0
  92. data/test/rails_root/vendor/plugins/plugin_with_macro/shoulda_macros/plugin_macro.rb +6 -0
  93. data/test/rspec_test.rb +207 -0
  94. data/test/test_helper.rb +3 -1
  95. data/test/unit/address_test.rb +1 -1
  96. data/test/unit/dog_test.rb +5 -2
  97. data/test/unit/post_test.rb +7 -3
  98. data/test/unit/product_test.rb +2 -2
  99. data/test/unit/tag_test.rb +2 -1
  100. data/test/unit/user_test.rb +27 -9
  101. metadata +82 -13
  102. data/lib/shoulda/controller.rb +0 -30
  103. data/lib/shoulda/controller/formats/html.rb +0 -201
  104. data/lib/shoulda/controller/formats/xml.rb +0 -170
  105. data/lib/shoulda/controller/helpers.rb +0 -64
  106. data/lib/shoulda/controller/macros.rb +0 -316
  107. data/lib/shoulda/controller/resource_options.rb +0 -236
  108. data/test/rails_root/app/models/dog.rb +0 -5
@@ -16,23 +16,4 @@ class UsersControllerTest < Test::Unit::TestCase
16
16
 
17
17
  should_filter_params :ssn
18
18
 
19
- should_be_restful do |resource|
20
- resource.identifier = :id
21
- resource.klass = User
22
- resource.object = :user
23
- resource.parent = []
24
- resource.actions = [:index, :show, :new, :edit, :update, :create, :destroy]
25
- resource.formats = [:html, :xml]
26
-
27
- resource.create.params = { :name => "bob", :email => 'bob@bob.com', :age => 13, :ssn => "123456789"}
28
- resource.update.params = { :name => "sue" }
29
-
30
- resource.create.redirect = "user_url(@user)"
31
- resource.update.redirect = "user_url(@user)"
32
- resource.destroy.redirect = "users_url"
33
-
34
- resource.create.flash = /created/i
35
- resource.update.flash = /updated/i
36
- resource.destroy.flash = /removed/i
37
- end
38
19
  end
@@ -0,0 +1,68 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class AllowMassAssignmentOfMatcherTest < Test::Unit::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,41 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class AllowValueMatcherTest < Test::Unit::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
+ end
@@ -0,0 +1,258 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class AssociationMatcherTest < Test::Unit::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), Parent.new
130
+ end
131
+
132
+ should "reject an association that has the wrong :through option" do
133
+ define_model :child
134
+ define_model :conception, :child_id => :integer,
135
+ :parent_id => :integer do
136
+ belongs_to :child
137
+ end
138
+ define_model :parent do
139
+ has_many :conceptions
140
+ has_many :children, :through => :conceptions
141
+ end
142
+ assert_rejects @matcher.through(:relationships), Parent.new
143
+ end
144
+
145
+ should "accept an association with a valid :dependent option" do
146
+ define_model :child, :parent_id => :integer
147
+ define_model :parent do
148
+ has_many :children, :dependent => :destroy
149
+ end
150
+ assert_accepts @matcher.dependent(:destroy), Parent.new
151
+ end
152
+
153
+ should "reject an association with a bad :dependent option" do
154
+ define_model :child, :parent_id => :integer
155
+ define_model :parent do
156
+ has_many :children
157
+ end
158
+ assert_rejects @matcher.dependent(:destroy), Parent.new
159
+ end
160
+ end
161
+
162
+ context "have_one" do
163
+ setup do
164
+ @matcher = have_one(:profile)
165
+ end
166
+
167
+ should "accept a valid association without any options" do
168
+ define_model :profile, :person_id => :integer
169
+ define_model :person do
170
+ has_one :profile
171
+ end
172
+ assert_accepts @matcher, Person.new
173
+ end
174
+
175
+ should "accept a valid association with an :as option" do
176
+ define_model :profile, :profilable_id => :integer,
177
+ :profilable_type => :string
178
+ define_model :person do
179
+ has_one :profile, :as => :profilable
180
+ end
181
+ assert_accepts @matcher, Person.new
182
+ end
183
+
184
+ should "reject an association that has a nonexistent foreign key" do
185
+ define_model :profile
186
+ define_model :person do
187
+ has_one :profile
188
+ end
189
+ assert_rejects @matcher, Person.new
190
+ end
191
+
192
+ should "reject an association with a bad :as option" do
193
+ define_model :profile, :profilable_id => :integer,
194
+ :profilable_type => :string
195
+ define_model :person do
196
+ has_one :profile, :as => :describable
197
+ end
198
+ assert_rejects @matcher, Person.new
199
+ end
200
+
201
+ should "accept an association with a valid :dependent option" do
202
+ define_model :profile, :person_id => :integer
203
+ define_model :person do
204
+ has_one :profile, :dependent => :destroy
205
+ end
206
+ assert_accepts @matcher.dependent(:destroy), Person.new
207
+ end
208
+
209
+ should "reject an association with a bad :dependent option" do
210
+ define_model :profile, :person_id => :integer
211
+ define_model :person do
212
+ has_one :profile
213
+ end
214
+ assert_rejects @matcher.dependent(:destroy), Person.new
215
+ end
216
+ end
217
+
218
+ context "have_and_belong_to_many" do
219
+ setup do
220
+ @matcher = have_and_belong_to_many(:relatives)
221
+ end
222
+
223
+ should "accept a valid association" do
224
+ define_model :relatives
225
+ define_model :person do
226
+ has_and_belongs_to_many :relatives
227
+ end
228
+ define_model :people_relative, :person_id => :integer,
229
+ :relative_id => :integer
230
+ assert_accepts @matcher, Person.new
231
+ end
232
+
233
+ should "reject a nonexistent association" do
234
+ define_model :relatives
235
+ define_model :person
236
+ define_model :people_relative, :person_id => :integer,
237
+ :relative_id => :integer
238
+ assert_rejects @matcher, Person.new
239
+ end
240
+
241
+ should "reject an association with a nonexistent join table" do
242
+ define_model :relatives
243
+ define_model :person do
244
+ has_and_belongs_to_many :relatives
245
+ end
246
+ assert_rejects @matcher, Person.new
247
+ end
248
+
249
+ should "reject an association of the wrong type" do
250
+ define_model :relatives, :person_id => :integer
251
+ define_model :person do
252
+ has_many :relatives
253
+ end
254
+ assert_rejects @matcher, Person.new
255
+ end
256
+ end
257
+
258
+ end
@@ -0,0 +1,80 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
+
3
+ class EnsureInclusionOfMatcherTest < Test::Unit::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