mongoid-rspec 3.0.0 → 4.1.0
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.
- checksums.yaml +5 -5
- data/LICENSE +1 -1
- data/README.md +220 -109
- data/Rakefile +8 -5
- data/lib/matchers/accept_nested_attributes.rb +22 -23
- data/lib/matchers/allow_mass_assignment.rb +12 -12
- data/lib/matchers/associations.rb +85 -49
- data/lib/matchers/be_dynamic_document.rb +26 -0
- data/lib/matchers/be_mongoid_document.rb +26 -0
- data/lib/matchers/be_stored_in.rb +55 -0
- data/lib/matchers/have_field.rb +90 -0
- data/lib/matchers/have_timestamps.rb +61 -0
- data/lib/matchers/indexes/have_index_for.rb +16 -0
- data/lib/matchers/indexes/v3/have_index_for.rb +59 -0
- data/lib/matchers/indexes/v4/have_index_for.rb +54 -0
- data/lib/matchers/validations/absence_of.rb +11 -0
- data/lib/matchers/validations/associated.rb +1 -1
- data/lib/matchers/validations/confirmation_of.rb +0 -7
- data/lib/matchers/validations/custom_validation_of.rb +1 -4
- data/lib/matchers/validations/exclusion_of.rb +5 -5
- data/lib/matchers/validations/format_of.rb +2 -2
- data/lib/matchers/validations/inclusion_of.rb +5 -5
- data/lib/matchers/validations/length_of.rb +13 -35
- data/lib/matchers/validations/numericality_of.rb +32 -16
- data/lib/matchers/validations/presence_of.rb +1 -1
- data/lib/matchers/validations/uniqueness_of.rb +7 -10
- data/lib/matchers/validations.rb +30 -11
- data/lib/mongoid/rspec/version.rb +2 -2
- data/lib/mongoid/rspec.rb +18 -5
- data/spec/models/article.rb +9 -6
- data/spec/models/comment.rb +1 -1
- data/spec/models/log.rb +3 -3
- data/spec/models/message.rb +17 -0
- data/spec/models/movie_article.rb +1 -2
- data/spec/models/person.rb +1 -1
- data/spec/models/profile.rb +2 -2
- data/spec/models/record.rb +1 -1
- data/spec/models/site.rb +5 -1
- data/spec/models/user.rb +12 -11
- data/spec/spec_helper.rb +9 -10
- data/spec/unit/accept_nested_attributes_spec.rb +1 -1
- data/spec/unit/associations_spec.rb +19 -7
- data/spec/unit/be_dynamic_document_spec.rb +21 -0
- data/spec/unit/be_mongoid_document_spec.rb +25 -0
- data/spec/unit/be_stored_in.rb +54 -0
- data/spec/unit/document_spec.rb +5 -14
- data/spec/unit/have_index_for_spec.rb +46 -0
- data/spec/unit/have_timestamps_spec.rb +71 -0
- data/spec/unit/validations_spec.rb +23 -15
- data/spec/validators/ssn_validator.rb +6 -6
- metadata +115 -39
- data/.document +0 -5
- data/.gitignore +0 -6
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
- data/.travis.yml +0 -10
- data/Gemfile +0 -4
- data/lib/matchers/collections.rb +0 -9
- data/lib/matchers/document.rb +0 -173
- data/lib/matchers/indexes.rb +0 -69
- data/lib/matchers/validations/with_message.rb +0 -27
- data/mongoid-rspec.gemspec +0 -26
- data/spec/unit/collections_spec.rb +0 -7
- data/spec/unit/indexes_spec.rb +0 -17
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6c59448b4e72ebb718332a28e7ddf77de5b792a09ea273b75959930e2c6c2918
|
|
4
|
+
data.tar.gz: b1818ed7a513db7a4e31bcdd5a492504a75becfc866dd95505192da1db7a56cc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 995c284d71146d52d554e8eff18e48605e76546ebce9428674d1fcefe4c560a73973651f4eb4496d21a839fb35944c37f98cc21eb78ddd1e1eea80a57d1392c4
|
|
7
|
+
data.tar.gz: a2e400ead572f31c614a294f1d5d10116e08fa6f152b2f58cf112da45608a3d72be2c5893ceb035c8a6f514af8ef55bcfdffd0a55a81a2e0f97ad43fd0699cf2
|
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -1,52 +1,218 @@
|
|
|
1
|
-
# [mongoid-rspec]
|
|
1
|
+
# [mongoid-rspec](https://github.com/mongoid/mongoid-rspec "A collection of RSpec-compatible matchers that help to test Mongoid documents.")
|
|
2
2
|
|
|
3
|
-
[![Build Status]
|
|
4
|
-
[![Gem Version]
|
|
5
|
-
[![Code Climate][codeclimate_badge]][codeclimate]
|
|
3
|
+
[](https://travis-ci.org/mongoid/mongoid-rspec)
|
|
4
|
+
[](https://badge.fury.io/rb/mongoid-rspec)
|
|
6
5
|
|
|
7
|
-
mongoid-rspec provides a collection of RSpec-compatible matchers that help to test Mongoid documents.
|
|
6
|
+
The mongoid-rspec library provides a collection of RSpec-compatible matchers that help to test Mongoid documents.
|
|
8
7
|
|
|
9
8
|
## Installation
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
Drop this line into your Gemfile:
|
|
12
11
|
|
|
13
|
-
|
|
12
|
+
```ruby
|
|
13
|
+
group :test do
|
|
14
|
+
gem 'mongoid-rspec'
|
|
15
|
+
end
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
```
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
## Compatibility
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
This gem is compatible with Mongoid 3, 4, 5, 6 and 7.
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
## Configuration
|
|
22
24
|
|
|
23
|
-
###
|
|
25
|
+
### Rails
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
Add to your `rails_helper.rb` file
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
```ruby
|
|
30
|
+
require 'mongoid-rspec'
|
|
28
31
|
|
|
29
|
-
|
|
32
|
+
RSpec.configure do |config|
|
|
33
|
+
config.include Mongoid::Matchers, type: :model
|
|
34
|
+
end
|
|
35
|
+
```
|
|
30
36
|
|
|
31
|
-
|
|
37
|
+
### Other
|
|
32
38
|
|
|
33
|
-
|
|
39
|
+
Add to your `spec_helper.rb` file.
|
|
34
40
|
|
|
35
|
-
|
|
41
|
+
```ruby
|
|
42
|
+
require 'mongoid-rspec'
|
|
36
43
|
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
RSpec.configure do |config|
|
|
45
|
+
config.include Mongoid::Matchers
|
|
46
|
+
end
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Matchers
|
|
50
|
+
|
|
51
|
+
### be_mongoid_document
|
|
39
52
|
|
|
40
53
|
```ruby
|
|
41
|
-
|
|
42
|
-
|
|
54
|
+
class Post
|
|
55
|
+
include Mongoid::Document
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
RSpec.describe Post, type: :model do
|
|
59
|
+
it { is_expected.to be_mongoid_document }
|
|
43
60
|
end
|
|
44
61
|
```
|
|
45
62
|
|
|
46
|
-
|
|
47
|
-
If you want to know why visit [the rspec documentation](https://relishapp.com/rspec/rspec-rails/docs/directory-structure).
|
|
63
|
+
### be_dynamic_document
|
|
48
64
|
|
|
49
|
-
|
|
65
|
+
```ruby
|
|
66
|
+
class User
|
|
67
|
+
include Mongoid::Document
|
|
68
|
+
include Mongoid::Attributes::Dynamic
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
RSpec.describe User, type: :model do
|
|
72
|
+
it { is_expected.to be_dynamic_document }
|
|
73
|
+
end
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### have_timestamps
|
|
77
|
+
|
|
78
|
+
With full timestamps.
|
|
79
|
+
|
|
80
|
+
```ruby
|
|
81
|
+
class Log
|
|
82
|
+
include Mongoid::Document
|
|
83
|
+
include Mongoid::Timestamps
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
RSpec.describe Log, type: :model do
|
|
87
|
+
it { is_expected.to have_timestamps }
|
|
88
|
+
end
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
With short timestamps.
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
class User
|
|
95
|
+
include Mongoid::Document
|
|
96
|
+
include Mongoid::Timestamps::Short
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
RSpec.describe User, type: :model do
|
|
100
|
+
it { is_expected.to have_timestamps.shortened }
|
|
101
|
+
end
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
With only creating or updating timestamps.
|
|
105
|
+
|
|
106
|
+
```ruby
|
|
107
|
+
class Admin
|
|
108
|
+
include Mongoid::Document
|
|
109
|
+
include Mongoid::Timestamps::Create
|
|
110
|
+
include Mongoid::Timestamps::Update
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
RSpec.describe Admin, type: :model do
|
|
114
|
+
it { is_expected.to have_timestamps.for(:creating) }
|
|
115
|
+
it { is_expected.to have_timestamps.for(:updating) }
|
|
116
|
+
end
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
With short creating or updating timestamps.
|
|
120
|
+
|
|
121
|
+
```ruby
|
|
122
|
+
class Post
|
|
123
|
+
include Mongoid::Document
|
|
124
|
+
include Mongoid::Timestamps::Create::Short
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
RSpec.describe Short, type: :model do
|
|
128
|
+
it { is_expected.to have_timestamps.for(:creating).shortened }
|
|
129
|
+
end
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### be_stored_in
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
class Post
|
|
136
|
+
include Mongoid::Document
|
|
137
|
+
|
|
138
|
+
store_in database: 'db1', collection: 'messages', client: 'secondary'
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
RSpec.describe Post, type: :model do
|
|
142
|
+
it { is_expected.to be_stored_in(database: 'db1', collection: 'messages', client: 'secondary') }
|
|
143
|
+
end
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
It checks only those options, that you specify. For instance, test in example below will pass, even though expectation contains only `database` option.
|
|
147
|
+
|
|
148
|
+
```ruby
|
|
149
|
+
class Comment
|
|
150
|
+
include Mongoid::Document
|
|
151
|
+
|
|
152
|
+
store_in database: 'db2', collection: 'messages'
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
RSpec.describe Comment, type: :model do
|
|
156
|
+
it { is_expected.to be_stored_in(database: 'db2') }
|
|
157
|
+
end
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
It works fine with lambdas and procs.
|
|
161
|
+
|
|
162
|
+
```ruby
|
|
163
|
+
class User
|
|
164
|
+
include Mongoid::Document
|
|
165
|
+
|
|
166
|
+
store_in database: ->{ Thread.current[:database] }
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
RSpec.describe Post, type: :model do
|
|
170
|
+
it do
|
|
171
|
+
Thread.current[:database] = 'db3'
|
|
172
|
+
is_expected.to be_stored_in(database: 'db3')
|
|
173
|
+
|
|
174
|
+
Thread.current[:database] = 'db4'
|
|
175
|
+
is_expected.to be_stored_in(database: 'db4')
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### have_index_for
|
|
181
|
+
|
|
182
|
+
```ruby
|
|
183
|
+
class Article
|
|
184
|
+
index({ title: 1 }, { unique: true, background: true, drop_dups: true })
|
|
185
|
+
index({ title: 1, created_at: -1 })
|
|
186
|
+
index({ category: 1 })
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
RSpec.describe Article, type: :model do
|
|
190
|
+
it do
|
|
191
|
+
is_expected
|
|
192
|
+
.to have_index_for(title: 1)
|
|
193
|
+
.with_options(unique: true, background: true, drop_dups: true)
|
|
194
|
+
end
|
|
195
|
+
it { is_expected.to have_index_for(title: 1, created_at: -1) }
|
|
196
|
+
it { is_expected.to have_index_for(category: 1) }
|
|
197
|
+
end
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### Field Matchers
|
|
201
|
+
|
|
202
|
+
```ruby
|
|
203
|
+
RSpec.describe Article do
|
|
204
|
+
it { is_expected.to have_field(:published).of_type(Boolean).with_default_value_of(false) }
|
|
205
|
+
it { is_expected.to have_field(:allow_comments).of_type(Boolean).with_default_value_of(true) }
|
|
206
|
+
it { is_expected.not_to have_field(:allow_comments).of_type(Boolean).with_default_value_of(false) }
|
|
207
|
+
it { is_expected.not_to have_field(:number_of_comments).of_type(Integer).with_default_value_of(1) }
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
RSpec.describe User do
|
|
211
|
+
it { is_expected.to have_fields(:email, :login) }
|
|
212
|
+
it { is_expected.to have_field(:s).with_alias(:status) }
|
|
213
|
+
it { is_expected.to have_fields(:birthdate, :registered_at).of_type(DateTime) }
|
|
214
|
+
end
|
|
215
|
+
```
|
|
50
216
|
|
|
51
217
|
### Association Matchers
|
|
52
218
|
|
|
@@ -55,14 +221,16 @@ RSpec.describe User do
|
|
|
55
221
|
it { is_expected.to have_many(:articles).with_foreign_key(:author_id).ordered_by(:title) }
|
|
56
222
|
|
|
57
223
|
it { is_expected.to have_one(:record) }
|
|
58
|
-
|
|
224
|
+
|
|
225
|
+
# can verify autobuild is set to true
|
|
59
226
|
it { is_expected.to have_one(:record).with_autobuild }
|
|
60
227
|
|
|
61
228
|
it { is_expected.to have_many :comments }
|
|
62
229
|
|
|
63
|
-
#can also specify with_dependent to test if :dependent => :destroy/:destroy_all/:delete is set
|
|
230
|
+
# can also specify with_dependent to test if :dependent => :destroy/:destroy_all/:delete is set
|
|
64
231
|
it { is_expected.to have_many(:comments).with_dependent(:destroy) }
|
|
65
|
-
|
|
232
|
+
|
|
233
|
+
# can verify autosave is set to true
|
|
66
234
|
it { is_expected.to have_many(:comments).with_autosave }
|
|
67
235
|
|
|
68
236
|
it { is_expected.to embed_one :profile }
|
|
@@ -93,20 +261,9 @@ end
|
|
|
93
261
|
RSpec.describe Site do
|
|
94
262
|
it { is_expected.to have_many(:users).as_inverse_of(:site).ordered_by(:email.asc).with_counter_cache }
|
|
95
263
|
end
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
### Mass Assignment Matcher
|
|
99
|
-
|
|
100
|
-
```ruby
|
|
101
|
-
RSpec.describe User do
|
|
102
|
-
it { is_expected.to allow_mass_assignment_of(:login) }
|
|
103
|
-
it { is_expected.to allow_mass_assignment_of(:email) }
|
|
104
|
-
it { is_expected.to allow_mass_assignment_of(:age) }
|
|
105
|
-
it { is_expected.to allow_mass_assignment_of(:password) }
|
|
106
|
-
it { is_expected.to allow_mass_assignment_of(:password) }
|
|
107
|
-
it { is_expected.to allow_mass_assignment_of(:role).as(:admin) }
|
|
108
264
|
|
|
109
|
-
|
|
265
|
+
RSpec.describe Message do
|
|
266
|
+
it { is_expected.to belong_to(:user).with_optional }
|
|
110
267
|
end
|
|
111
268
|
```
|
|
112
269
|
|
|
@@ -137,6 +294,11 @@ end
|
|
|
137
294
|
|
|
138
295
|
RSpec.describe Article do
|
|
139
296
|
it { is_expected.to validate_length_of(:title).within(8..16) }
|
|
297
|
+
it { is_expected.to validate_absence_of(:deletion_date) }
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
RSpec.describe Visitor do
|
|
301
|
+
it { is_expected.to validate_length_of(:name).with_maximum(160).with_minimum(1) }
|
|
140
302
|
end
|
|
141
303
|
|
|
142
304
|
RSpec.describe Profile do
|
|
@@ -155,91 +317,40 @@ RSpec.describe Person do
|
|
|
155
317
|
end
|
|
156
318
|
```
|
|
157
319
|
|
|
158
|
-
###
|
|
320
|
+
### Mass Assignment Matcher
|
|
159
321
|
|
|
160
322
|
```ruby
|
|
161
323
|
RSpec.describe User do
|
|
162
|
-
it { is_expected.to
|
|
163
|
-
it { is_expected.to
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
it { is_expected.to
|
|
168
|
-
end
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
### Index Matcher
|
|
172
|
-
|
|
173
|
-
```ruby
|
|
174
|
-
RSpec.describe Article do
|
|
175
|
-
it { is_expected.to have_index_for(published: 1) }
|
|
176
|
-
it { is_expected.to have_index_for(title: 1).with_options(unique: true, background: true) }
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
RSpec.describe Profile do
|
|
180
|
-
it { is_expected.to have_index_for(first_name: 1, last_name: 1) }
|
|
181
|
-
end
|
|
324
|
+
it { is_expected.to allow_mass_assignment_of(:login) }
|
|
325
|
+
it { is_expected.to allow_mass_assignment_of(:email) }
|
|
326
|
+
it { is_expected.to allow_mass_assignment_of(:age) }
|
|
327
|
+
it { is_expected.to allow_mass_assignment_of(:password) }
|
|
328
|
+
it { is_expected.to allow_mass_assignment_of(:password) }
|
|
329
|
+
it { is_expected.to allow_mass_assignment_of(:role).as(:admin) }
|
|
182
330
|
|
|
183
|
-
|
|
184
|
-
it { is_expected.to have_index_for(created_at: 1).with_options(bucket_size: 100, expire_after_seconds: 3600) }
|
|
331
|
+
it { is_expected.not_to allow_mass_assignment_of(:role) }
|
|
185
332
|
end
|
|
186
333
|
```
|
|
187
334
|
|
|
188
|
-
###
|
|
335
|
+
### Accepts Nested Attributes Matcher
|
|
189
336
|
|
|
190
337
|
```ruby
|
|
191
338
|
RSpec.describe User do
|
|
192
|
-
it { is_expected.to
|
|
193
|
-
it { is_expected.to
|
|
194
|
-
it { is_expected.to have_fields(:birthdate, :registered_at).of_type(DateTime) }
|
|
195
|
-
|
|
196
|
-
# if you're declaring 'include Mongoid::Timestamps'
|
|
197
|
-
# or any of 'include Mongoid::Timestamps::Created' and 'Mongoid::Timestamps::Updated'
|
|
198
|
-
it { is_expected.to be_timestamped_document }
|
|
199
|
-
it { is_expected.to be_timestamped_document.with(:created) }
|
|
200
|
-
it { is_expected.not_to be_timestamped_document.with(:updated) }
|
|
201
|
-
|
|
202
|
-
it { is_expected.to be_versioned_document } # if you're declaring `include Mongoid::Versioning`
|
|
203
|
-
it { is_expected.to be_paranoid_document } # if you're declaring `include Mongoid::Paranoia`
|
|
204
|
-
it { is_expected.to be_multiparameted_document } # if you're declaring `include Mongoid::MultiParameterAttributes`
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
RSpec.describe Log do
|
|
208
|
-
it { is_expected.to be_stored_in :logs }
|
|
209
|
-
it { is_expected.to be_dynamic_document }
|
|
339
|
+
it { is_expected.to accept_nested_attributes_for(:articles) }
|
|
340
|
+
it { is_expected.to accept_nested_attributes_for(:comments) }
|
|
210
341
|
end
|
|
211
342
|
|
|
212
343
|
RSpec.describe Article do
|
|
213
|
-
it { is_expected.to
|
|
214
|
-
it { is_expected.to have_field(:allow_comments).of_type(Boolean).with_default_value_of(true) }
|
|
215
|
-
it { is_expected.not_to have_field(:allow_comments).of_type(Boolean).with_default_value_of(false) }
|
|
216
|
-
it { is_expected.not_to have_field(:number_of_comments).of_type(Integer).with_default_value_of(1) }
|
|
344
|
+
it { is_expected.to accept_nested_attributes_for(:permalink) }
|
|
217
345
|
end
|
|
218
346
|
```
|
|
219
347
|
|
|
220
|
-
##
|
|
221
|
-
|
|
222
|
-
accept_nested_attributes_for matcher must test options [issue 91](https://github.com/mongoid-rspec/mongoid-rspec/issues/91).
|
|
223
|
-
|
|
224
|
-
## Acknowledgement
|
|
225
|
-
|
|
226
|
-
Thanks to [Durran Jordan][durran] for providing the changes necessary to make
|
|
227
|
-
this compatible with mongoid 2.0.0.rc, and for other [contributors](https://github.com/mongoid-rspec/mongoid-rspec/contributors)
|
|
228
|
-
to this project.
|
|
229
|
-
|
|
230
|
-
[mongoid-rspec]: https://github.com/mongoid-rspec/mongoid-rspec "A collection of RSpec-compatible matchers that help to test Mongoid documents."
|
|
348
|
+
## Contributing
|
|
231
349
|
|
|
232
|
-
[
|
|
233
|
-
[mongoid2]: https://rubygems.org/gems/mongoid-rspec/versions/1.4.5
|
|
234
|
-
[mongoid3]: https://rubygems.org/gems/mongoid-rspec/versions/1.13.0
|
|
235
|
-
[mongoid4]: https://rubygems.org/gems/mongoid-rspec/versions/2.1.0
|
|
236
|
-
[mongoid5]: https://rubygems.org/gems/mongoid-rspec/versions/3.0.0
|
|
350
|
+
You're encouraged to contribute to this library. See [CONTRIBUTING](CONTRIBUTING.md) for details.
|
|
237
351
|
|
|
238
|
-
|
|
239
|
-
[travis]: https://travis-ci.org/mongoid-rspec/mongoid-rspec
|
|
352
|
+
## Copyright and License
|
|
240
353
|
|
|
241
|
-
|
|
242
|
-
[rubygems]: http://rubygems.org/gems/mongoid-rspec
|
|
354
|
+
Copyright (c) 2009-2018 Evan Sagge and Contributors.
|
|
243
355
|
|
|
244
|
-
[
|
|
245
|
-
[codeclimate]: https://codeclimate.com/github/mongoid-rspec/mongoid-rspec
|
|
356
|
+
MIT License. See [LICENSE](LICENSE) for details.
|
data/Rakefile
CHANGED
|
@@ -1,17 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
|
2
2
|
|
|
3
3
|
require 'bundler'
|
|
4
4
|
Bundler::GemHelper.install_tasks
|
|
5
5
|
|
|
6
6
|
require 'rspec/core/rake_task'
|
|
7
7
|
|
|
8
|
-
task :default => :spec
|
|
9
|
-
|
|
10
8
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
11
|
-
spec.pattern =
|
|
9
|
+
spec.pattern = './spec/**/*_spec.rb'
|
|
12
10
|
end
|
|
13
11
|
|
|
14
12
|
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
|
15
|
-
spec.pattern =
|
|
13
|
+
spec.pattern = './spec/**/*_spec.rb'
|
|
16
14
|
spec.rcov = true
|
|
17
15
|
end
|
|
16
|
+
|
|
17
|
+
require 'rubocop/rake_task'
|
|
18
|
+
RuboCop::RakeTask.new(:rubocop)
|
|
19
|
+
|
|
20
|
+
task default: %i[rubocop spec]
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
module Mongoid
|
|
2
2
|
module Matchers # :nodoc:
|
|
3
|
-
|
|
4
3
|
# Ensures that the model can accept nested attributes for the specified
|
|
5
4
|
# association.
|
|
6
5
|
#
|
|
@@ -12,7 +11,6 @@ module Mongoid
|
|
|
12
11
|
end
|
|
13
12
|
|
|
14
13
|
class AcceptNestedAttributesForMatcher
|
|
15
|
-
|
|
16
14
|
def initialize(attribute)
|
|
17
15
|
@attribute = attribute.to_s
|
|
18
16
|
@options = {}
|
|
@@ -31,37 +29,38 @@ module Mongoid
|
|
|
31
29
|
"Did not expect #{expectation}"
|
|
32
30
|
end
|
|
33
31
|
|
|
34
|
-
alias
|
|
32
|
+
alias failure_message_when_negated negative_failure_message
|
|
35
33
|
|
|
36
34
|
def description
|
|
37
35
|
description = "accepts_nested_attributes_for :#{@attribute}"
|
|
38
36
|
end
|
|
39
37
|
|
|
40
38
|
protected
|
|
41
|
-
def match?
|
|
42
|
-
exists?
|
|
43
|
-
end
|
|
44
39
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
else
|
|
49
|
-
@problem = 'is not declared'
|
|
50
|
-
false
|
|
51
|
-
end
|
|
52
|
-
end
|
|
40
|
+
def match?
|
|
41
|
+
exists?
|
|
42
|
+
end
|
|
53
43
|
|
|
54
|
-
|
|
55
|
-
|
|
44
|
+
def exists?
|
|
45
|
+
if config
|
|
46
|
+
true
|
|
47
|
+
else
|
|
48
|
+
@problem = 'is not declared'
|
|
49
|
+
false
|
|
56
50
|
end
|
|
51
|
+
end
|
|
57
52
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
53
|
+
def config
|
|
54
|
+
model_class.nested_attributes["#{@attribute}_attributes"]
|
|
55
|
+
end
|
|
61
56
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
57
|
+
def model_class
|
|
58
|
+
@subject.class
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def expectation
|
|
62
|
+
"#{model_class.name} to accept nested attributes for #{@attribute}"
|
|
63
|
+
end
|
|
65
64
|
end
|
|
66
65
|
end
|
|
67
|
-
end
|
|
66
|
+
end
|
|
@@ -3,7 +3,7 @@ module Mongoid
|
|
|
3
3
|
module Matchers
|
|
4
4
|
class AllowMassAssignmentOfMatcher # :nodoc:
|
|
5
5
|
attr_reader :failure_message, :negative_failure_message
|
|
6
|
-
alias
|
|
6
|
+
alias failure_message_when_negated negative_failure_message
|
|
7
7
|
|
|
8
8
|
def initialize(attribute)
|
|
9
9
|
@attribute = attribute.to_s
|
|
@@ -12,7 +12,7 @@ module Mongoid
|
|
|
12
12
|
|
|
13
13
|
def as(role)
|
|
14
14
|
if active_model_less_than_3_1?
|
|
15
|
-
raise
|
|
15
|
+
raise 'You can specify role only in Rails 3.1 or greater'
|
|
16
16
|
end
|
|
17
17
|
@options[:role] = role
|
|
18
18
|
self
|
|
@@ -25,20 +25,20 @@ module Mongoid
|
|
|
25
25
|
@negative_failure_message = "#{@attribute} was made accessible"
|
|
26
26
|
else
|
|
27
27
|
if protected_attributes.empty?
|
|
28
|
-
@negative_failure_message =
|
|
28
|
+
@negative_failure_message = 'no attributes were protected'
|
|
29
29
|
else
|
|
30
|
-
@negative_failure_message = "#{class_name} is protecting "
|
|
31
|
-
|
|
32
|
-
|
|
30
|
+
@negative_failure_message = "#{class_name} is protecting " \
|
|
31
|
+
"#{protected_attributes.to_a.to_sentence}, " \
|
|
32
|
+
"but not #{@attribute}."
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
true
|
|
36
36
|
else
|
|
37
|
-
if whitelisting?
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
@failure_message = if whitelisting?
|
|
38
|
+
"Expected #{@attribute} to be accessible"
|
|
39
|
+
else
|
|
40
|
+
"Did not expect #{@attribute} to be protected"
|
|
41
|
+
end
|
|
42
42
|
false
|
|
43
43
|
end
|
|
44
44
|
end
|
|
@@ -62,7 +62,7 @@ module Mongoid
|
|
|
62
62
|
end
|
|
63
63
|
|
|
64
64
|
def whitelisting?
|
|
65
|
-
authorizer.
|
|
65
|
+
authorizer.is_a?(::ActiveModel::MassAssignmentSecurity::WhiteList)
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
def attr_mass_assignable?
|