factory_bot 6.3.0 → 6.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/GETTING_STARTED.md +41 -4
- data/NEWS.md +8 -0
- data/lib/factory_bot/strategy/stub.rb +14 -3
- data/lib/factory_bot/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96b5b25a348686d9d0e394b4a8de682a4fb49fe83ae4038f758905c50cd2d57b
|
4
|
+
data.tar.gz: 169f33bb5c12fed9548be6b010a06b0b5e71e6867950ec2a6344d63d82b75605
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e238bf844535aa25a1fa01097ebfe49df0fe5edca86d4e4eb64d970f8bd47bfe90ee125596c1411ce0df52cb3d018466170da866b82fe24a7fa4f9e9e97acc9d
|
7
|
+
data.tar.gz: cd1ece3fad8c3325d5da64103705e132c291934438a58638e5d966289815be6b8cda0552f2d040a04d9924d66107f92ccb1c50978ee1d82df87302428aefe2a4
|
data/GETTING_STARTED.md
CHANGED
@@ -285,6 +285,9 @@ user = create(:user)
|
|
285
285
|
# Returns a hash of attributes that can be used to build a User instance
|
286
286
|
attrs = attributes_for(:user)
|
287
287
|
|
288
|
+
# Integrates with Ruby 3.0's support for pattern matching assignment
|
289
|
+
attributes_for(:user) => {email:, name:, **attrs}
|
290
|
+
|
288
291
|
# Returns an object with all defined attributes stubbed out
|
289
292
|
stub = build_stubbed(:user)
|
290
293
|
|
@@ -297,7 +300,7 @@ end
|
|
297
300
|
### Attribute overrides
|
298
301
|
|
299
302
|
No matter which strategy is used, it's possible to override the defined
|
300
|
-
attributes by passing a
|
303
|
+
attributes by passing a Hash:
|
301
304
|
|
302
305
|
```ruby
|
303
306
|
# Build a User instance and override the first_name property
|
@@ -306,6 +309,29 @@ user.first_name
|
|
306
309
|
# => "Joe"
|
307
310
|
```
|
308
311
|
|
312
|
+
Overriding associations is also supported:
|
313
|
+
|
314
|
+
```ruby
|
315
|
+
account = build(:account, :deluxe)
|
316
|
+
friends = build_list(:user, 2)
|
317
|
+
|
318
|
+
user = build(:user, account: account, friends: friends)
|
319
|
+
```
|
320
|
+
|
321
|
+
Ruby 3.1's support for [omitting values][] from `Hash` literals dovetails with
|
322
|
+
attribute overrides and provides an opportunity to limit the repetition of
|
323
|
+
variable names:
|
324
|
+
|
325
|
+
```ruby
|
326
|
+
account = build(:account, :deluxe)
|
327
|
+
friends = build_list(:user, 2)
|
328
|
+
|
329
|
+
# The keyword arguments correspond to local variable names, so omit their values
|
330
|
+
user = build(:user, account:, friends:)
|
331
|
+
```
|
332
|
+
|
333
|
+
[omitting values]: https://docs.ruby-lang.org/en/3.1/syntax/literals_rdoc.html#label-Hash+Literals
|
334
|
+
|
309
335
|
### `build_stubbed` and `Marshal.dump`
|
310
336
|
|
311
337
|
Note that objects created with `build_stubbed` cannot be serialized with
|
@@ -945,7 +971,7 @@ end
|
|
945
971
|
Note that this approach works with `build`, `build_stubbed`, and `create`, but
|
946
972
|
the associations will return `nil` when using `attributes_for`.
|
947
973
|
|
948
|
-
Also, note that if you assign any attributes inside a custom `initialize_with`
|
974
|
+
Also, note that if you assign any attributes inside a custom `initialize_with`
|
949
975
|
(e.g. `initialize_with { new(**attributes) }`), those attributes should not refer to `instance`,
|
950
976
|
since it will be `nil`.
|
951
977
|
|
@@ -1008,6 +1034,17 @@ factory :user do
|
|
1008
1034
|
end
|
1009
1035
|
```
|
1010
1036
|
|
1037
|
+
With Ruby 2.7's support for [numbered parameters][], inline definitions can be
|
1038
|
+
even more abbreviated:
|
1039
|
+
|
1040
|
+
```ruby
|
1041
|
+
factory :user do
|
1042
|
+
sequence(:email) { "person#{_1}@example.com" }
|
1043
|
+
end
|
1044
|
+
```
|
1045
|
+
|
1046
|
+
[numbered parameters]: https://ruby-doc.org/core-2.7.1/Proc.html#class-Proc-label-Numbered+parameters
|
1047
|
+
|
1011
1048
|
### Initial value
|
1012
1049
|
|
1013
1050
|
You can override the initial value. Any value that responds to the `#next`
|
@@ -1222,11 +1259,11 @@ FactoryBot.define do
|
|
1222
1259
|
created_at { 8.days.ago }
|
1223
1260
|
updated_at { 4.days.ago }
|
1224
1261
|
end
|
1225
|
-
|
1262
|
+
|
1226
1263
|
factory :user, traits: [:timestamps] do
|
1227
1264
|
username { "john_doe" }
|
1228
1265
|
end
|
1229
|
-
|
1266
|
+
|
1230
1267
|
factory :post do
|
1231
1268
|
timestamps
|
1232
1269
|
title { "Traits rock" }
|
data/NEWS.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# News
|
2
2
|
|
3
|
+
## 6.4.0 (November 17, 2023)
|
4
|
+
|
5
|
+
* Added: if `build_stubbed` detects a UUID primary key, generate the correct
|
6
|
+
type (Peter Boling, Alexandre Ruban).
|
7
|
+
* Docs: show examples of Ruby 3 syntactic sugars (Sean Doyle).
|
8
|
+
* Internal: resolve test warning messages (Mike Burns).
|
9
|
+
|
10
|
+
|
3
11
|
## 6.3.0 (September 1, 2023)
|
4
12
|
|
5
13
|
* Fix: link to changelog for RubyGems (Berkan Ünal).
|
@@ -47,13 +47,17 @@ module FactoryBot
|
|
47
47
|
|
48
48
|
private
|
49
49
|
|
50
|
-
def next_id
|
51
|
-
|
50
|
+
def next_id(result_instance)
|
51
|
+
if uuid_primary_key?(result_instance)
|
52
|
+
SecureRandom.uuid
|
53
|
+
else
|
54
|
+
@@next_id += 1
|
55
|
+
end
|
52
56
|
end
|
53
57
|
|
54
58
|
def stub_database_interaction_on_result(result_instance)
|
55
59
|
if has_settable_id?(result_instance)
|
56
|
-
result_instance.id ||= next_id
|
60
|
+
result_instance.id ||= next_id(result_instance)
|
57
61
|
end
|
58
62
|
|
59
63
|
result_instance.instance_eval do
|
@@ -83,6 +87,13 @@ module FactoryBot
|
|
83
87
|
result_instance.class.primary_key
|
84
88
|
end
|
85
89
|
|
90
|
+
def uuid_primary_key?(result_instance)
|
91
|
+
result_instance.respond_to?(:column_for_attribute) &&
|
92
|
+
(column = result_instance.column_for_attribute(result_instance.class.primary_key)) &&
|
93
|
+
column.respond_to?(:sql_type) &&
|
94
|
+
column.sql_type == "uuid"
|
95
|
+
end
|
96
|
+
|
86
97
|
def clear_changes_information(result_instance)
|
87
98
|
if result_instance.respond_to?(:clear_changes_information)
|
88
99
|
result_instance.clear_changes_information
|
data/lib/factory_bot/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_bot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.
|
4
|
+
version: 6.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Clayton
|
8
8
|
- Joe Ferris
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2023-
|
12
|
+
date: 2023-11-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -238,7 +238,7 @@ licenses:
|
|
238
238
|
- MIT
|
239
239
|
metadata:
|
240
240
|
changelog_uri: https://github.com/thoughtbot/factory_bot/blob/main/NEWS.md
|
241
|
-
post_install_message:
|
241
|
+
post_install_message:
|
242
242
|
rdoc_options: []
|
243
243
|
require_paths:
|
244
244
|
- lib
|
@@ -253,8 +253,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
253
253
|
- !ruby/object:Gem::Version
|
254
254
|
version: '0'
|
255
255
|
requirements: []
|
256
|
-
rubygems_version: 3.4.
|
257
|
-
signing_key:
|
256
|
+
rubygems_version: 3.4.10
|
257
|
+
signing_key:
|
258
258
|
specification_version: 4
|
259
259
|
summary: factory_bot provides a framework and DSL for defining and using model instance
|
260
260
|
factories.
|