shoulda-matchers 1.4.1 → 1.4.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.
@@ -1,8 +1,12 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do
4
+ it "should state in its description that it allows only numeric values" do
5
+ matcher = Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher.new(:attr)
6
+ matcher.description.should == "only allow numeric values for attr"
7
+ end
4
8
 
5
- context "a numeric attribute" do
9
+ context "given a numeric attribute" do
6
10
  before do
7
11
  define_model :example, :attr => :string do
8
12
  validates_numericality_of :attr
@@ -11,28 +15,60 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do
11
15
  end
12
16
 
13
17
  it "should only allow numeric values for that attribute" do
14
- @model.should validate_numericality_of(:attr)
18
+ matcher = new_matcher(:attr)
19
+ matcher.matches?(@model).should be_true
15
20
  end
16
21
 
17
22
  it "should not override the default message with a blank" do
18
- @model.should validate_numericality_of(:attr).with_message(nil)
23
+ matcher = new_matcher(:attr)
24
+ matcher.with_message(nil)
25
+ matcher.matches?(@model).should be_true
26
+ end
27
+
28
+ context "when asked to enforce integer values for that attribute" do
29
+ it "should not match" do
30
+ matcher = new_matcher(:attr)
31
+ matcher.only_integer
32
+ matcher.matches?(@model).should be_false
33
+ end
34
+
35
+ it "should fail with the ActiveRecord :not_an_integer message" do
36
+ matcher = new_matcher(:attr)
37
+ matcher.only_integer
38
+ matcher.matches?(@model)
39
+ matcher.failure_message.should include 'Expected errors to include "must be an integer"'
40
+ end
19
41
  end
20
42
  end
21
43
 
22
- context "a numeric attribute which must be integer" do
44
+ context "given a numeric attribute which must be integer" do
23
45
  before do
24
46
  define_model :example, :attr => :string do
25
- validates_numericality_of :attr, { :only_integer => true }
47
+ validates_numericality_of :attr, { :only_integer => true }
26
48
  end
27
49
  @model = Example.new
28
50
  end
29
51
 
30
- it "should only allow integer values for that attribute" do
31
- @model.should validate_numericality_of(:attr).only_integer
52
+ it "allows integer values for that attribute" do
53
+ matcher = new_matcher(:attr)
54
+ matcher.only_integer
55
+ matcher.matches?(@model).should be_true
56
+ end
57
+
58
+ it "does not allow non-integer values for that attribute" do
59
+ matcher = new_matcher(:attr)
60
+ matcher.only_integer
61
+ matcher.matches?(@model).should be_true
62
+ end
63
+
64
+ it "should state in its description that it allows only integer values" do
65
+ matcher = new_matcher(:attr)
66
+ matcher.only_integer
67
+ matcher.description.should == "only allow numeric, integer values for attr"
32
68
  end
33
69
  end
34
70
 
35
- context "a numeric attribute with a custom validation message" do
71
+ context "given a numeric attribute with a custom validation message" do
36
72
  before do
37
73
  define_model :example, :attr => :string do
38
74
  validates_numericality_of :attr, :message => 'custom'
@@ -41,22 +77,36 @@ describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do
41
77
  end
42
78
 
43
79
  it "should only allow numeric values for that attribute with that message" do
44
- @model.should validate_numericality_of(:attr).with_message(/custom/)
80
+ matcher = new_matcher(:attr)
81
+ matcher.with_message(/custom/)
82
+ matcher.matches?(@model).should be_true
45
83
  end
46
84
 
47
85
  it "should not allow numeric values for that attribute with another message" do
48
- @model.should_not validate_numericality_of(:attr)
86
+ matcher = new_matcher(:attr)
87
+ matcher.with_message(/wrong/)
88
+ matcher.matches?(@model).should be_false
49
89
  end
50
90
  end
51
91
 
52
- context "a non-numeric attribute" do
92
+ context "given a non-numeric attribute" do
53
93
  before do
54
94
  @model = define_model(:example, :attr => :string).new
55
95
  end
56
96
 
57
97
  it "should not only allow numeric values for that attribute" do
58
- @model.should_not validate_numericality_of(:attr)
98
+ matcher = new_matcher(:attr)
99
+ matcher.matches?(@model).should be_false
100
+ end
101
+
102
+ it "should fail with the ActiveRecord :not_a_number message" do
103
+ matcher = new_matcher(:attr)
104
+ matcher.matches?(@model)
105
+ matcher.failure_message.should include 'Expected errors to include "is not a number"'
59
106
  end
60
107
  end
61
108
 
109
+ def new_matcher(attr)
110
+ Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher.new(attr)
111
+ end
62
112
  end
@@ -0,0 +1,183 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::Independent::DelegateMatcher do
4
+ it 'supports chaining on #to' do
5
+ matcher = delegate_method(:method)
6
+ matcher.to(:another_method).should == matcher
7
+ end
8
+
9
+ it 'supports chaining on #with_arguments' do
10
+ matcher = delegate_method(:method)
11
+ matcher.with_arguments(1, 2, 3).should == matcher
12
+ end
13
+
14
+ it 'supports chaining on #as' do
15
+ matcher = delegate_method(:method)
16
+ matcher.as(:some_other_method).should == matcher
17
+ end
18
+
19
+ it 'should raise an error if no delegation target is defined' do
20
+ object = Object.new
21
+ expect {
22
+ object.should delegate_method(:name)
23
+ }.to raise_exception Shoulda::Matchers::Independent::DelegateMatcher::TargetNotDefinedError
24
+ end
25
+
26
+ it 'should raise an error if called with #should_not' do
27
+ object = Object.new
28
+ expect {
29
+ object.should_not delegate_method(:name).to(:anyone)
30
+ }.to raise_exception Shoulda::Matchers::Independent::DelegateMatcher::InvalidDelegateMatcher
31
+ end
32
+
33
+ context 'given a method that does not delegate' do
34
+ before do
35
+ define_class(:post_office) do
36
+ def deliver_mail
37
+ :delivered
38
+ end
39
+ end
40
+ end
41
+
42
+ it 'does not match' do
43
+ post_office = PostOffice.new
44
+ matcher = delegate_method(:deliver_mail).to(:mailman)
45
+ matcher.matches?(post_office).should be_false
46
+ end
47
+
48
+ it 'has a failure message that indicates which method should have been delegated' do
49
+ post_office = PostOffice.new
50
+ matcher = delegate_method(:deliver_mail).to(:mailman)
51
+
52
+ matcher.matches?(post_office)
53
+
54
+ message = 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman'
55
+ matcher.failure_message.should == message
56
+ end
57
+
58
+ it 'uses the proper syntax for class methods in errors' do
59
+ matcher = delegate_method(:deliver_mail).to(:mailman)
60
+
61
+ matcher.matches?(PostOffice)
62
+
63
+ message = 'Expected PostOffice.deliver_mail to delegate to PostOffice.mailman'
64
+ matcher.failure_message.should == message
65
+ end
66
+ end
67
+
68
+ context 'given a method that delegates properly' do
69
+ before do
70
+ define_class(:mailman)
71
+ define_class(:post_office) do
72
+ def deliver_mail
73
+ mailman.deliver_mail
74
+ end
75
+
76
+ def mailman
77
+ Mailman.new
78
+ end
79
+ end
80
+ end
81
+
82
+ it 'matches' do
83
+ post_office = PostOffice.new
84
+ post_office.should delegate_method(:deliver_mail).to(:mailman)
85
+ end
86
+ end
87
+
88
+ context 'given a method that delegates properly with certain arguments' do
89
+ before do
90
+ define_class(:mailman)
91
+ define_class(:post_office) do
92
+ def deliver_mail
93
+ mailman.deliver_mail('221B Baker St.', :hastily => true)
94
+ end
95
+
96
+ def mailman
97
+ Mailman.new
98
+ end
99
+ end
100
+ end
101
+
102
+ context 'when given the correct arguments' do
103
+ it 'matches' do
104
+ post_office = PostOffice.new
105
+ matcher = delegate_method(:deliver_mail).to(:mailman).with_arguments('221B Baker St.', :hastily => true)
106
+ post_office.should matcher
107
+ end
108
+ end
109
+
110
+ context 'when not given the correct arguments' do
111
+ it 'does not match' do
112
+ post_office = PostOffice.new
113
+ matcher = delegate_method(:deliver_mail).to(:mailman).with_arguments('123 Nowhere Ln.')
114
+ matcher.matches?(post_office).should be_false
115
+ end
116
+
117
+ it 'has a failure message that indicates which arguments were expected' do
118
+ post_office = PostOffice.new
119
+ matcher = delegate_method(:deliver_mail).to(:mailman).with_arguments('123 Nowhere Ln.')
120
+
121
+ matcher.matches?(post_office)
122
+
123
+ message = 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman with arguments: ["123 Nowhere Ln."]'
124
+ matcher.failure_message.should == message
125
+ end
126
+ end
127
+ end
128
+
129
+ context 'given a method that delegates properly to a method of a different name' do
130
+ before do
131
+ define_class(:mailman)
132
+ define_class(:post_office) do
133
+ def deliver_mail
134
+ mailman.deliver_mail_and_avoid_dogs
135
+ end
136
+
137
+ def mailman
138
+ Mailman.new
139
+ end
140
+ end
141
+ end
142
+
143
+ context 'when given the correct method name' do
144
+ it 'matches' do
145
+ post_office = PostOffice.new
146
+ matcher = delegate_method(:deliver_mail).to(:mailman).as(:deliver_mail_and_avoid_dogs)
147
+ post_office.should matcher
148
+ end
149
+ end
150
+
151
+ context 'when given an incorrect method name' do
152
+ it 'does not match' do
153
+ post_office = PostOffice.new
154
+ matcher = delegate_method(:deliver_mail).to(:mailman).as(:watch_tv)
155
+ matcher.matches?(post_office).should be_false
156
+ end
157
+
158
+ it 'has a failure message that indicates which method was expected' do
159
+ post_office = PostOffice.new
160
+ matcher = delegate_method(:deliver_mail).to(:mailman).as(:watch_tv)
161
+
162
+ matcher.matches?(post_office)
163
+
164
+ message = 'Expected PostOffice#deliver_mail to delegate to PostOffice#mailman as :watch_tv'
165
+ matcher.failure_message.should == message
166
+ end
167
+ end
168
+ end
169
+ end
170
+
171
+ describe Shoulda::Matchers::Independent::DelegateMatcher::TargetNotDefinedError do
172
+ it 'has a useful message' do
173
+ error = Shoulda::Matchers::Independent::DelegateMatcher::TargetNotDefinedError.new
174
+ error.message.should include 'Delegation needs a target'
175
+ end
176
+ end
177
+
178
+ describe Shoulda::Matchers::Independent::DelegateMatcher::InvalidDelegateMatcher do
179
+ it 'has a useful message' do
180
+ error = Shoulda::Matchers::Independent::DelegateMatcher::InvalidDelegateMatcher.new
181
+ error.message.should include 'does not support #should_not'
182
+ end
183
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shoulda-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2012-10-15 00:00:00.000000000 Z
17
+ date: 2012-11-30 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: activesupport
@@ -33,53 +33,53 @@ dependencies:
33
33
  - !ruby/object:Gem::Version
34
34
  version: 3.0.0
35
35
  - !ruby/object:Gem::Dependency
36
- name: appraisal
36
+ name: bourne
37
37
  requirement: !ruby/object:Gem::Requirement
38
38
  none: false
39
39
  requirements:
40
40
  - - ~>
41
41
  - !ruby/object:Gem::Version
42
- version: 0.4.0
43
- type: :development
42
+ version: 1.1.2
43
+ type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  none: false
47
47
  requirements:
48
48
  - - ~>
49
49
  - !ruby/object:Gem::Version
50
- version: 0.4.0
50
+ version: 1.1.2
51
51
  - !ruby/object:Gem::Dependency
52
- name: aruba
52
+ name: appraisal
53
53
  requirement: !ruby/object:Gem::Requirement
54
54
  none: false
55
55
  requirements:
56
- - - ! '>='
56
+ - - ~>
57
57
  - !ruby/object:Gem::Version
58
- version: '0'
58
+ version: 0.4.0
59
59
  type: :development
60
60
  prerelease: false
61
61
  version_requirements: !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
- - - ! '>='
64
+ - - ~>
65
65
  - !ruby/object:Gem::Version
66
- version: '0'
66
+ version: 0.4.0
67
67
  - !ruby/object:Gem::Dependency
68
- name: bourne
68
+ name: aruba
69
69
  requirement: !ruby/object:Gem::Requirement
70
70
  none: false
71
71
  requirements:
72
- - - ~>
72
+ - - ! '>='
73
73
  - !ruby/object:Gem::Version
74
- version: 1.1.2
74
+ version: '0'
75
75
  type: :development
76
76
  prerelease: false
77
77
  version_requirements: !ruby/object:Gem::Requirement
78
78
  none: false
79
79
  requirements:
80
- - - ~>
80
+ - - ! '>='
81
81
  - !ruby/object:Gem::Version
82
- version: 1.1.2
82
+ version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: bundler
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -203,12 +203,14 @@ files:
203
203
  - lib/shoulda/matchers/active_model.rb
204
204
  - lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb
205
205
  - lib/shoulda/matchers/active_model/allow_value_matcher.rb
206
+ - lib/shoulda/matchers/active_model/disallow_value_matcher.rb
206
207
  - lib/shoulda/matchers/active_model/ensure_exclusion_of_matcher.rb
207
208
  - lib/shoulda/matchers/active_model/ensure_inclusion_of_matcher.rb
208
209
  - lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb
209
210
  - lib/shoulda/matchers/active_model/errors.rb
210
211
  - lib/shoulda/matchers/active_model/exception_message_finder.rb
211
212
  - lib/shoulda/matchers/active_model/helpers.rb
213
+ - lib/shoulda/matchers/active_model/only_integer_matcher.rb
212
214
  - lib/shoulda/matchers/active_model/validate_acceptance_of_matcher.rb
213
215
  - lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb
214
216
  - lib/shoulda/matchers/active_model/validate_format_of_matcher.rb
@@ -226,6 +228,8 @@ files:
226
228
  - lib/shoulda/matchers/active_record/query_the_database_matcher.rb
227
229
  - lib/shoulda/matchers/active_record/serialize_matcher.rb
228
230
  - lib/shoulda/matchers/assertion_error.rb
231
+ - lib/shoulda/matchers/independent.rb
232
+ - lib/shoulda/matchers/independent/delegate_matcher.rb
229
233
  - lib/shoulda/matchers/integrations/rspec.rb
230
234
  - lib/shoulda/matchers/integrations/test_unit.rb
231
235
  - lib/shoulda/matchers/version.rb
@@ -250,11 +254,13 @@ files:
250
254
  - spec/shoulda/action_mailer/have_sent_email_spec.rb
251
255
  - spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb
252
256
  - spec/shoulda/active_model/allow_value_matcher_spec.rb
257
+ - spec/shoulda/active_model/disallow_value_matcher_spec.rb
253
258
  - spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb
254
259
  - spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb
255
260
  - spec/shoulda/active_model/ensure_length_of_matcher_spec.rb
256
261
  - spec/shoulda/active_model/exception_message_finder_spec.rb
257
262
  - spec/shoulda/active_model/helpers_spec.rb
263
+ - spec/shoulda/active_model/only_integer_matcher_spec.rb
258
264
  - spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb
259
265
  - spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb
260
266
  - spec/shoulda/active_model/validate_format_of_matcher_spec.rb
@@ -269,6 +275,7 @@ files:
269
275
  - spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb
270
276
  - spec/shoulda/active_record/query_the_database_matcher_spec.rb
271
277
  - spec/shoulda/active_record/serialize_matcher_spec.rb
278
+ - spec/shoulda/independent/delegate_matcher_spec.rb
272
279
  - spec/spec_helper.rb
273
280
  - spec/support/active_model_versions.rb
274
281
  - spec/support/class_builder.rb
@@ -289,7 +296,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
289
296
  version: '0'
290
297
  segments:
291
298
  - 0
292
- hash: 2974632708122422691
299
+ hash: 3885961739329133579
293
300
  required_rubygems_version: !ruby/object:Gem::Requirement
294
301
  none: false
295
302
  requirements:
@@ -298,7 +305,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
298
305
  version: '0'
299
306
  segments:
300
307
  - 0
301
- hash: 2974632708122422691
308
+ hash: 3885961739329133579
302
309
  requirements: []
303
310
  rubyforge_project:
304
311
  rubygems_version: 1.8.24
@@ -329,11 +336,13 @@ test_files:
329
336
  - spec/shoulda/action_mailer/have_sent_email_spec.rb
330
337
  - spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb
331
338
  - spec/shoulda/active_model/allow_value_matcher_spec.rb
339
+ - spec/shoulda/active_model/disallow_value_matcher_spec.rb
332
340
  - spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb
333
341
  - spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb
334
342
  - spec/shoulda/active_model/ensure_length_of_matcher_spec.rb
335
343
  - spec/shoulda/active_model/exception_message_finder_spec.rb
336
344
  - spec/shoulda/active_model/helpers_spec.rb
345
+ - spec/shoulda/active_model/only_integer_matcher_spec.rb
337
346
  - spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb
338
347
  - spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb
339
348
  - spec/shoulda/active_model/validate_format_of_matcher_spec.rb
@@ -348,6 +357,7 @@ test_files:
348
357
  - spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb
349
358
  - spec/shoulda/active_record/query_the_database_matcher_spec.rb
350
359
  - spec/shoulda/active_record/serialize_matcher_spec.rb
360
+ - spec/shoulda/independent/delegate_matcher_spec.rb
351
361
  - spec/spec_helper.rb
352
362
  - spec/support/active_model_versions.rb
353
363
  - spec/support/class_builder.rb