shoulda-matchers 4.3.0 → 4.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +157 -77
- data/lib/shoulda/matchers/active_model/numericality_matchers/comparison_matcher.rb +5 -1
- data/lib/shoulda/matchers/active_model/validate_inclusion_of_matcher.rb +2 -21
- data/lib/shoulda/matchers/active_model/validate_length_of_matcher.rb +27 -3
- data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +32 -0
- data/lib/shoulda/matchers/active_model/validation_matcher.rb +25 -0
- data/lib/shoulda/matchers/active_record.rb +2 -0
- data/lib/shoulda/matchers/active_record/association_matcher.rb +20 -4
- data/lib/shoulda/matchers/active_record/association_matchers/join_table_matcher.rb +2 -2
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflection.rb +12 -6
- data/lib/shoulda/matchers/active_record/association_matchers/model_reflector.rb +20 -3
- data/lib/shoulda/matchers/active_record/have_attached_matcher.rb +147 -0
- data/lib/shoulda/matchers/active_record/have_implicit_order_column.rb +106 -0
- data/lib/shoulda/matchers/active_record/have_secure_token_matcher.rb +28 -9
- data/lib/shoulda/matchers/active_record/validate_uniqueness_of_matcher.rb +2 -2
- data/lib/shoulda/matchers/independent.rb +0 -1
- data/lib/shoulda/matchers/rails_shim.rb +4 -0
- data/lib/shoulda/matchers/util.rb +9 -2
- data/lib/shoulda/matchers/util/word_wrap.rb +1 -1
- data/lib/shoulda/matchers/version.rb +1 -1
- metadata +8 -8
- data/MIT-LICENSE +0 -22
- data/lib/shoulda/matchers/independent/delegate_method_matcher/stubbed_target.rb +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f12034724c2954cf58fc7e89723ce42119e3a6f39676899c0510545a61f5fee
|
4
|
+
data.tar.gz: 0c5cd62168c0dd00fa3246c9afe2ee4499db8c37312ff6f0f8ee10afb16f0f42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e33ed5061a6913d3b448f2f251e79a7618c9226275a308bf5a0547a81dddb4fbaf17a8834ddf3a1e3662cc712afaf6994bb9ea44a727adcdffbd41653ced13c
|
7
|
+
data.tar.gz: e7ca6969c599eeb2fa8a084a4486085b6156760d0a77a1466d3989c0a9f15b7b092d7cdb7f4e2bbdf1befd5437afbd69121316e2ba6d9b271c809e3abfad82d1
|
data/README.md
CHANGED
@@ -22,10 +22,10 @@ complex, and error-prone.
|
|
22
22
|
## Quick links
|
23
23
|
|
24
24
|
📖 **[Read the documentation for the latest version][rubydocs].**
|
25
|
-
📢 **[See what's changed in
|
25
|
+
📢 **[See what's changed in recent versions][changelog].**
|
26
26
|
|
27
27
|
[rubydocs]: http://matchers.shoulda.io/docs
|
28
|
-
[
|
28
|
+
[changelog]: CHANGELOG.md
|
29
29
|
|
30
30
|
## Table of contents
|
31
31
|
|
@@ -41,10 +41,12 @@ complex, and error-prone.
|
|
41
41
|
* [ActiveRecord matchers](#activerecord-matchers)
|
42
42
|
* [ActionController matchers](#actioncontroller-matchers)
|
43
43
|
* [Independent matchers](#independent-matchers)
|
44
|
-
* [
|
44
|
+
* [Extensions](#extensions)
|
45
45
|
* [Contributing](#contributing)
|
46
|
+
* [Compatibility](#compatibility)
|
46
47
|
* [Versioning](#versioning)
|
47
|
-
* [
|
48
|
+
* [Team](#team)
|
49
|
+
* [Copyright/License](#copyright-license)
|
48
50
|
* [About thoughtbot](#about-thoughtbot)
|
49
51
|
|
50
52
|
## Getting started
|
@@ -55,21 +57,21 @@ Start by including `shoulda-matchers` in your Gemfile:
|
|
55
57
|
|
56
58
|
```ruby
|
57
59
|
group :test do
|
58
|
-
gem 'shoulda-matchers'
|
60
|
+
gem 'shoulda-matchers', '~> 4.0'
|
59
61
|
end
|
60
62
|
```
|
61
63
|
|
62
|
-
|
64
|
+
Then run `bundle install`.
|
63
65
|
|
64
|
-
|
65
|
-
* Which portion of the matchers you want to use
|
66
|
+
Now you need to configure the gem by telling it:
|
66
67
|
|
67
|
-
|
68
|
-
|
68
|
+
* which matchers you want to use in your tests
|
69
|
+
* that you're using RSpec so that it can make those matchers available in
|
70
|
+
your example groups
|
69
71
|
|
70
72
|
#### Rails apps
|
71
73
|
|
72
|
-
|
74
|
+
If you're working on a Rails app, simply place this at the bottom of
|
73
75
|
`spec/rails_helper.rb` (or in a support file if you so choose):
|
74
76
|
|
75
77
|
```ruby
|
@@ -81,13 +83,12 @@ Shoulda::Matchers.configure do |config|
|
|
81
83
|
end
|
82
84
|
```
|
83
85
|
|
84
|
-
Now you're ready to [use matchers in your tests](#usage)!
|
85
|
-
|
86
86
|
#### Non-Rails apps
|
87
87
|
|
88
|
-
If
|
89
|
-
ActiveModel, you can still use this gem too! In that case,
|
90
|
-
the following configuration at the bottom of
|
88
|
+
If you're not working on a Rails app, but you still make use of ActiveRecord or
|
89
|
+
ActiveModel in your project, you can still use this gem too! In that case,
|
90
|
+
you'll want to place the following configuration at the bottom of
|
91
|
+
`spec/spec_helper.rb`:
|
91
92
|
|
92
93
|
```ruby
|
93
94
|
Shoulda::Matchers.configure do |config|
|
@@ -101,45 +102,83 @@ Shoulda::Matchers.configure do |config|
|
|
101
102
|
end
|
102
103
|
```
|
103
104
|
|
104
|
-
Now you're ready to [use matchers in your tests](#usage)!
|
105
|
-
|
106
105
|
### Minitest
|
107
106
|
|
108
|
-
|
109
|
-
|
110
|
-
|
107
|
+
If you're using our umbrella gem [Shoulda], then make sure that you're using the
|
108
|
+
latest version:
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
group :test do
|
112
|
+
gem 'shoulda', '~> 4.0'
|
113
|
+
end
|
114
|
+
```
|
111
115
|
|
112
|
-
[
|
113
|
-
[shoulda-context]: https://github.com/thoughtbot/shoulda-context
|
116
|
+
[Shoulda]: https://github.com/thoughtbot/shoulda
|
114
117
|
|
115
|
-
|
116
|
-
and 4.x, so you'll want to add the following to your Gemfile:
|
118
|
+
Otherwise, add `shoulda-matchers` to your Gemfile:
|
117
119
|
|
118
120
|
```ruby
|
119
121
|
group :test do
|
120
|
-
gem 'shoulda', '~>
|
121
|
-
gem 'shoulda-matchers', '~> 2.0'
|
122
|
-
gem 'rails-controller-testing'
|
122
|
+
gem 'shoulda-matchers', '~> 4.0'
|
123
123
|
end
|
124
124
|
```
|
125
125
|
|
126
|
-
|
126
|
+
Then run `bundle install`.
|
127
|
+
|
128
|
+
Now you need to configure the gem by telling it:
|
129
|
+
|
130
|
+
* which matchers you want to use in your tests
|
131
|
+
* that you're using Minitest so that it can make those matchers available in
|
132
|
+
your test case classes
|
133
|
+
|
134
|
+
#### Rails apps
|
135
|
+
|
136
|
+
If you're working on a Rails app, simply place this at the bottom of
|
137
|
+
`test/test_helper.rb`:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
Shoulda::Matchers.configure do |config|
|
141
|
+
config.integrate do |with|
|
142
|
+
with.test_framework :minitest
|
143
|
+
with.library :rails
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
#### Non-Rails apps
|
149
|
+
|
150
|
+
If you're not working on a Rails app, but you still make use of ActiveRecord or
|
151
|
+
ActiveModel in your project, you can still use this gem too! In that case,
|
152
|
+
you'll want to place the following configuration at the bottom of
|
153
|
+
`test/test_helper.rb`:
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
Shoulda::Matchers.configure do |config|
|
157
|
+
config.integrate do |with|
|
158
|
+
with.test_framework :minitest
|
159
|
+
|
160
|
+
# Keep as many of these lines as are necessary:
|
161
|
+
with.library :active_record
|
162
|
+
with.library :active_model
|
163
|
+
end
|
164
|
+
end
|
165
|
+
```
|
127
166
|
|
128
167
|
## Usage
|
129
168
|
|
130
|
-
|
131
|
-
|
169
|
+
Most of the matchers provided by this gem are useful in a Rails context, and as
|
170
|
+
such, can be used for different parts of a Rails app:
|
132
171
|
|
133
172
|
* [database models backed by ActiveRecord](#activemodel-matchers)
|
134
173
|
* [non-database models, form objects, etc. backed by
|
135
174
|
ActiveModel](#activerecord-matchers)
|
136
175
|
* [controllers](#actioncontroller-matchers)
|
137
176
|
* [routes](#routing-matchers) (RSpec only)
|
138
|
-
* [
|
177
|
+
* [Rails-specific features like `delegate`](#independent-matchers)
|
139
178
|
|
140
|
-
|
141
|
-
|
142
|
-
test case may look something like:
|
179
|
+
As the name of the gem indicates, most matchers are designed to be used in
|
180
|
+
"one-liner" form using the `should` macro, a special directive available in both
|
181
|
+
RSpec and [Shoulda]. For instance, a model test case may look something like:
|
143
182
|
|
144
183
|
``` ruby
|
145
184
|
# RSpec
|
@@ -167,16 +206,17 @@ class MenuItemTest < ActiveSupport::TestCase
|
|
167
206
|
end
|
168
207
|
```
|
169
208
|
|
170
|
-
|
209
|
+
[See below](#matchers) for the full set of matchers that you can use.
|
171
210
|
|
172
211
|
### On the subject of `subject`
|
173
212
|
|
174
213
|
For both RSpec and Shoulda, the **subject** is an implicit reference to the
|
175
|
-
object under test, and
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
214
|
+
object under test, and through the use of `should` as demonstrated above, all of
|
215
|
+
the matchers make use of `subject` internally when they are run. A `subject` is
|
216
|
+
always set automatically by your test framework in any given test case; however,
|
217
|
+
in certain cases it can be advantageous to override it. For instance, when
|
218
|
+
testing validations in a model, it is customary to provide a valid model instead
|
219
|
+
of a fresh one:
|
180
220
|
|
181
221
|
``` ruby
|
182
222
|
# RSpec
|
@@ -234,11 +274,19 @@ end
|
|
234
274
|
|
235
275
|
### Availability of RSpec matchers in example groups
|
236
276
|
|
277
|
+
#### Rails projects
|
278
|
+
|
237
279
|
If you're using RSpec, then you're probably familiar with the concept of example
|
238
|
-
groups
|
239
|
-
|
240
|
-
|
241
|
-
groups
|
280
|
+
groups. Example groups can be assigned tags order to assign different behavior
|
281
|
+
to different kinds of example groups. This comes into play especially when using
|
282
|
+
`rspec-rails`, where, for instance, controller example groups, tagged with
|
283
|
+
`type: :controller`, are written differently than request example groups, tagged
|
284
|
+
with `type: :request`. This difference in writing style arises because
|
285
|
+
`rspec-rails` mixes different behavior and methods into controller example
|
286
|
+
groups vs. request example groups.
|
287
|
+
|
288
|
+
Relying on this behavior, Shoulda Matchers automatically makes certain matchers
|
289
|
+
available in certain kinds of example groups:
|
242
290
|
|
243
291
|
* ActiveRecord and ActiveModel matchers are available only in model example
|
244
292
|
groups, i.e., those tagged with `type: :model` or in files located under
|
@@ -250,13 +298,27 @@ groups:
|
|
250
298
|
tagged with `type: :routing` or in files located under `spec/routing`.
|
251
299
|
* Independent matchers are available in all example groups.
|
252
300
|
|
253
|
-
As long as you're using Rails, you don't need to worry about
|
254
|
-
should "just work".
|
301
|
+
As long as you're using Rails, you don't need to worry about these details —
|
302
|
+
everything should "just work".
|
303
|
+
|
304
|
+
#### Non-Rails projects
|
305
|
+
|
306
|
+
**What if you are using ActiveModel or ActiveRecord outside of Rails, however,
|
307
|
+
and you want to use model matchers in a certain example group?** Then you'll
|
308
|
+
need to manually include the module that holds those matchers into that example
|
309
|
+
group. For instance, you might have to say:
|
255
310
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
311
|
+
``` ruby
|
312
|
+
RSpec.describe MySpecialModel do
|
313
|
+
include Shoulda::Matchers::ActiveModel
|
314
|
+
include Shoulda::Matchers::ActiveRecord
|
315
|
+
end
|
316
|
+
```
|
317
|
+
|
318
|
+
If you have a lot of similar example groups in which you need to do this, then
|
319
|
+
you might find it more helpful to tag your example groups appropriately, then
|
320
|
+
instruct RSpec to mix these modules into any example groups that have that tag.
|
321
|
+
For instance, you could add this to your `rails_helper.rb`:
|
260
322
|
|
261
323
|
```ruby
|
262
324
|
RSpec.configure do |config|
|
@@ -265,21 +327,20 @@ RSpec.configure do |config|
|
|
265
327
|
end
|
266
328
|
```
|
267
329
|
|
268
|
-
|
330
|
+
And from then on, you could say:
|
269
331
|
|
270
332
|
```ruby
|
271
|
-
describe MySpecialModel, type: :model do
|
333
|
+
RSpec.describe MySpecialModel, type: :model do
|
272
334
|
# ...
|
273
335
|
end
|
274
336
|
```
|
275
337
|
|
276
338
|
### `should` vs `is_expected.to`
|
277
339
|
|
278
|
-
In this README and throughout the documentation,
|
279
|
-
of RSpec's one-liner syntax over `is_expected.to`.
|
280
|
-
|
281
|
-
|
282
|
-
can do that too:
|
340
|
+
In this README and throughout the documentation, you'll notice that we use the
|
341
|
+
`should` form of RSpec's one-liner syntax over `is_expected.to`. Beside being
|
342
|
+
the namesake of the gem itself, this is our preferred syntax as it's short and
|
343
|
+
sweet. But if you prefer to use `is_expected.to`, you can do that too:
|
283
344
|
|
284
345
|
```ruby
|
285
346
|
RSpec.describe Person, type: :model do
|
@@ -289,7 +350,7 @@ end
|
|
289
350
|
|
290
351
|
## Matchers
|
291
352
|
|
292
|
-
|
353
|
+
Here is the full list of matchers that ship with this gem. If you need details
|
293
354
|
about any of them, make sure to [consult the documentation][rubydocs]!
|
294
355
|
|
295
356
|
### ActiveModel matchers
|
@@ -330,10 +391,16 @@ about any of them, make sure to [consult the documentation][rubydocs]!
|
|
330
391
|
tests that the table that backs your model has a specific column.
|
331
392
|
* **[have_db_index](lib/shoulda/matchers/active_record/have_db_index_matcher.rb)**
|
332
393
|
tests that the table that backs your model has an index on a specific column.
|
394
|
+
* **[have_implicit_order_column](lib/shoulda/matchers/active_record/have_implicit_order_column.rb)**
|
395
|
+
tests usage of `implicit_order_column`.
|
333
396
|
* **[have_many](lib/shoulda/matchers/active_record/association_matcher.rb)**
|
334
397
|
tests your `has_many` associations.
|
398
|
+
* **[have_many_attached](lib/shoulda/matchers/active_record/have_attached_matcher.rb)**
|
399
|
+
tests your `has_many_attached` associations.
|
335
400
|
* **[have_one](lib/shoulda/matchers/active_record/association_matcher.rb)**
|
336
401
|
tests your `has_one` associations.
|
402
|
+
* **[have_one_attached](lib/shoulda/matchers/active_record/have_attached_matcher.rb)**
|
403
|
+
tests your `has_one_attached` associations.
|
337
404
|
* **[have_readonly_attribute](lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb)**
|
338
405
|
tests usage of the `attr_readonly` macro.
|
339
406
|
* **[have_rich_text](lib/shoulda/matchers/active_record/have_rich_text_matcher.rb)**
|
@@ -383,37 +450,51 @@ about any of them, make sure to [consult the documentation][rubydocs]!
|
|
383
450
|
tests that an object forwards messages to other, internal objects by way of
|
384
451
|
delegation.
|
385
452
|
|
386
|
-
##
|
453
|
+
## Extensions
|
387
454
|
|
388
|
-
|
389
|
-
|
455
|
+
Over time our community has created extensions to Shoulda Matchers. If you've
|
456
|
+
created something that you want to share, please [let us know][new-issue]!
|
390
457
|
|
391
|
-
|
458
|
+
* **[shoulda-matchers-cucumber]** – Adds support for using Shoulda Matchers in
|
459
|
+
Cucumber tests.
|
392
460
|
|
393
|
-
[
|
461
|
+
[new-issue]: https://github.com/thoughtbot/shoulda-matchers/issues/new
|
462
|
+
[shoulda-matchers-cucumber]: https://github.com/majioa/shoulda-matchers-cucumber
|
394
463
|
|
395
464
|
## Contributing
|
396
465
|
|
397
|
-
|
398
|
-
|
466
|
+
Have a fix for a problem you've been running into or an idea for a new feature
|
467
|
+
you think would be useful? Take a look at the [Contributing
|
468
|
+
document](CONTRIBUTING.md) for instructions on setting up the repo on your
|
469
|
+
machine, understanding the codebase, and creating a good pull request.
|
399
470
|
|
400
|
-
|
471
|
+
## Compatibility
|
401
472
|
|
402
|
-
|
403
|
-
|
404
|
-
|
473
|
+
Shoulda Matchers is [tested][travis] and supported against Ruby 2.4+, Rails
|
474
|
+
4.2+, RSpec 3.x, and Minitest 5.x.
|
475
|
+
|
476
|
+
For Ruby < 2.4 and Rails < 4.1 compatibility, please use [v3.1.3][v3.1.3].
|
477
|
+
|
478
|
+
[v3.1.3]: https://github.com/thoughtbot/shoulda-matchers/tree/v3.1.3
|
405
479
|
|
406
480
|
## Versioning
|
407
481
|
|
408
482
|
Shoulda Matchers follows Semantic Versioning 2.0 as defined at
|
409
483
|
<http://semver.org>.
|
410
484
|
|
411
|
-
##
|
485
|
+
## Team
|
486
|
+
|
487
|
+
Shoulda Matchers is maintained by [Elliot Winkler][mcmire] and [Gui
|
488
|
+
Albuk][guialbuk].
|
489
|
+
|
490
|
+
[mcmire]: https://github.com/mcmire
|
491
|
+
[guialbuk]: https://github.com/guialbuk
|
492
|
+
|
493
|
+
## Copyright/License
|
412
494
|
|
413
|
-
Shoulda Matchers is copyright © 2006-2020
|
414
|
-
|
415
|
-
|
416
|
-
[MIT-LICENSE](MIT-LICENSE) file.
|
495
|
+
Shoulda Matchers is copyright © 2006-2020 Tammer Saleh and [thoughtbot,
|
496
|
+
inc][thoughtbot-website]. It is free and opensource software and may be
|
497
|
+
redistributed under the terms specified in the [LICENSE](LICENSE) file.
|
417
498
|
|
418
499
|
[thoughtbot-website]: https://thoughtbot.com
|
419
500
|
|
@@ -423,8 +504,7 @@ and may be redistributed under the terms specified in the
|
|
423
504
|
|
424
505
|
[thoughtbot-logo]: https://presskit.thoughtbot.com/images/thoughtbot-logo-for-readmes.svg
|
425
506
|
|
426
|
-
|
427
|
-
logos for thoughtbot are trademarks of thoughtbot, inc.
|
507
|
+
The names and logos for thoughtbot are trademarks of thoughtbot, inc.
|
428
508
|
|
429
509
|
We are passionate about open source software. See [our other
|
430
510
|
projects][community]. We are [available for hire][hire].
|
@@ -9,7 +9,8 @@ module Shoulda
|
|
9
9
|
:>= => :greater_than_or_equal_to,
|
10
10
|
:< => :less_than,
|
11
11
|
:<= => :less_than_or_equal_to,
|
12
|
-
:== => :equal_to
|
12
|
+
:== => :equal_to,
|
13
|
+
:!= => :other_than,
|
13
14
|
}
|
14
15
|
|
15
16
|
def initialize(numericality_matcher, value, operator)
|
@@ -125,6 +126,8 @@ module Shoulda
|
|
125
126
|
[true, false, false]
|
126
127
|
when :<=
|
127
128
|
[true, true, false]
|
129
|
+
when :!=
|
130
|
+
[true, false, true]
|
128
131
|
end
|
129
132
|
end
|
130
133
|
|
@@ -146,6 +149,7 @@ module Shoulda
|
|
146
149
|
when :== then "equal to"
|
147
150
|
when :< then "less than"
|
148
151
|
when :<= then "less than or equal to"
|
152
|
+
when :!= then 'other than'
|
149
153
|
end
|
150
154
|
end
|
151
155
|
end
|
@@ -311,15 +311,6 @@ EOT
|
|
311
311
|
self
|
312
312
|
end
|
313
313
|
|
314
|
-
def allow_blank
|
315
|
-
@options[:allow_blank] = true
|
316
|
-
self
|
317
|
-
end
|
318
|
-
|
319
|
-
def expects_to_allow_blank?
|
320
|
-
@options[:allow_blank]
|
321
|
-
end
|
322
|
-
|
323
314
|
def allow_nil
|
324
315
|
@options[:allow_nil] = true
|
325
316
|
self
|
@@ -423,14 +414,14 @@ EOT
|
|
423
414
|
allows_all_values_in_array? &&
|
424
415
|
disallows_all_values_outside_of_array? &&
|
425
416
|
allows_nil_value? &&
|
426
|
-
|
417
|
+
allow_blank_matches?
|
427
418
|
end
|
428
419
|
|
429
420
|
def does_not_match_for_array?
|
430
421
|
disallows_any_values_in_array? ||
|
431
422
|
allows_any_value_outside_of_array? ||
|
432
423
|
disallows_nil_value? ||
|
433
|
-
|
424
|
+
allow_blank_does_not_match?
|
434
425
|
end
|
435
426
|
|
436
427
|
def allows_lower_value
|
@@ -616,16 +607,6 @@ EOT
|
|
616
607
|
@options[:allow_nil] && disallows_value_of(nil)
|
617
608
|
end
|
618
609
|
|
619
|
-
def allows_blank_value?
|
620
|
-
@options[:allow_blank] != true ||
|
621
|
-
BLANK_VALUES.all? { |value| allows_value_of(value) }
|
622
|
-
end
|
623
|
-
|
624
|
-
def disallows_blank_value?
|
625
|
-
@options[:allow_blank] &&
|
626
|
-
BLANK_VALUES.any? { |value| disallows_value_of(value) }
|
627
|
-
end
|
628
|
-
|
629
610
|
def inspected_array
|
630
611
|
Shoulda::Matchers::Util.inspect_values(@array).to_sentence(
|
631
612
|
two_words_connector: " or ",
|