factory_bot-refinements 0.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 +7 -0
- data/README.md +101 -0
- data/lib/factory_bot/refinements/active_record.rb +37 -0
- data/lib/factory_bot/refinements/rspec.rb +49 -0
- data/lib/factory_bot/refinements/version.rb +5 -0
- data/lib/factory_bot/refinements.rb +25 -0
- data/lib/factory_bot-refinements.rb +1 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f97ca8bc54ccac42b61ddc6aa2f3e9ec9239d0e385015bdbfb6822f53403a146
|
4
|
+
data.tar.gz: fb5851e3d397a28c1b782809bc42827b19755fa39d81b2b9759175b5fa6e9ed5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1d6e43bc05498885ccfed186f7c725899a42b3db86ac89e579b80220343ad4dfb6c50da612bf370cbc16eec74bef5c0bea3389959c99b4dc259004a4c32ae397
|
7
|
+
data.tar.gz: 568ac0d452823c63835b72933a64e359837a6ba458a293135a2ebe0b237505a698fefac25ba4edc5632c4c008bd71bf1d1a89f069f8f731f23d61007016833c6
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# FactoryBot::Refinements
|
2
|
+
|
3
|
+
Extensions for factory_bot
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'factory_bot-refinements'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install factory_bot-refinements
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Add the following code to your test file:
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
# ActiveRecord extensions
|
27
|
+
using FactoryBot::Refinements::ActiveRecord
|
28
|
+
|
29
|
+
# RSpec extensions
|
30
|
+
using FactoryBot::Refinements::RSpec
|
31
|
+
|
32
|
+
# Or both of them
|
33
|
+
using FactoryBot::Refinements
|
34
|
+
```
|
35
|
+
|
36
|
+
### FactoryBot::Refinements::ActiveRecord
|
37
|
+
|
38
|
+
Add a set of methods to create/build a record using a factory for `has_many` associations. All arguments, including keyword arguments and a block, will be delegated to the factory.
|
39
|
+
|
40
|
+
By default, the factory to use will be automatically looked up from the association's class. You can also specify an arbitrary one with `factory()`.
|
41
|
+
|
42
|
+
|
43
|
+
``` ruby
|
44
|
+
using FactoryBot::Refinements::ActiveRecord
|
45
|
+
|
46
|
+
describe MyApp do
|
47
|
+
example do
|
48
|
+
user = create(:user)
|
49
|
+
|
50
|
+
user.posts.create_with_factory(:published, title: 'hello')
|
51
|
+
# Equivalent to:
|
52
|
+
# FactoryBot.create(:post, :published, user: user, title: 'hello')
|
53
|
+
|
54
|
+
user.posts.factory(:featured_post).create_with_factory
|
55
|
+
# Equivalent to:
|
56
|
+
# FactoryBot.create(:featured_post, user: user)
|
57
|
+
|
58
|
+
user.posts.build_with_factory
|
59
|
+
# Equivalent to:
|
60
|
+
# FactoryBot.build(:post, user: user)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
### FactoryBot::Refinements::RSpec
|
66
|
+
|
67
|
+
Add a set of short-hand methods to the describe/context block that do `FactoryBot.create/build` and `let/let!` together. All arguments except the first, including keyword arguments and a block, will be delegated to the factory as-is.
|
68
|
+
|
69
|
+
``` ruby
|
70
|
+
using FactoryBot::Refinements::RSpec
|
71
|
+
|
72
|
+
describe MyApp do
|
73
|
+
create :user, name: 'alice'
|
74
|
+
# Equivalent to:
|
75
|
+
# let!(:user) { FactoryBot.create(:user, name: 'alice') }
|
76
|
+
|
77
|
+
create :user.as(:bob), :admin, name: 'bob'
|
78
|
+
# Equivalent to:
|
79
|
+
# let!(:bob) { FactoryBot.create(:user, :admin, name: 'bob') }
|
80
|
+
|
81
|
+
create_lazy :user
|
82
|
+
# Equivalent to (let, not let!):
|
83
|
+
# let(:user) { FactoryBot.create(:user) }
|
84
|
+
|
85
|
+
build :user
|
86
|
+
# Equivalent to:
|
87
|
+
# let!(:user) { FactoryBot.build(:user) }
|
88
|
+
|
89
|
+
build_lazy :user
|
90
|
+
# Equivalent to:
|
91
|
+
# let(:user) { FactoryBot.build(:user) }
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
#### Limitations
|
96
|
+
|
97
|
+
Due to scope constraints, these factories do not have access to other `let` values. Therefore, it is recommended to use them for fixture-like fixed data setups. If you need `let` to build associations, consider using [RSpec::LetAs](https://github.com/ursm/rspec-let_as).
|
98
|
+
|
99
|
+
## License
|
100
|
+
|
101
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'factory_bot'
|
3
|
+
|
4
|
+
module FactoryBot
|
5
|
+
module Refinements
|
6
|
+
module ActiveRecord
|
7
|
+
def self.default_factory(association)
|
8
|
+
association.klass.model_name.singular
|
9
|
+
end
|
10
|
+
|
11
|
+
refine ::ActiveRecord::Associations::CollectionProxy do
|
12
|
+
def factory(name)
|
13
|
+
tap { @__factory = name }
|
14
|
+
end
|
15
|
+
|
16
|
+
def create_with_factory(*args, **kwargs, &block)
|
17
|
+
factory = @__factory || ActiveRecord.default_factory(proxy_association)
|
18
|
+
key = proxy_association.reflection.inverse_of.name
|
19
|
+
|
20
|
+
FactoryBot.create(factory, *args, key => proxy_association.owner, **kwargs, &block).tap {
|
21
|
+
@__factory = nil
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
def build_with_factory(...)
|
26
|
+
factory = @__factory || ActiveRecord.default_factory(proxy_association)
|
27
|
+
|
28
|
+
FactoryBot.build(factory, ...).tap {|record|
|
29
|
+
proxy_association.add_to_target record
|
30
|
+
|
31
|
+
@__factory = nil
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'factory_bot'
|
2
|
+
require 'rspec/core'
|
3
|
+
|
4
|
+
module FactoryBot
|
5
|
+
module Refinements
|
6
|
+
module RSpec
|
7
|
+
def self.normalize_name_and_factory(name_and_factory)
|
8
|
+
case name_and_factory
|
9
|
+
when Array
|
10
|
+
name_and_factory
|
11
|
+
else
|
12
|
+
[name_and_factory, name_and_factory]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
refine Symbol do
|
17
|
+
def as(as)
|
18
|
+
[as, self]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
refine ::RSpec::Core::ExampleGroup.singleton_class do
|
23
|
+
def create(name_and_factory, ...)
|
24
|
+
name, factory = RSpec.normalize_name_and_factory(name_and_factory)
|
25
|
+
|
26
|
+
let!(name) { FactoryBot.create(factory, ...) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def create_lazy(name_and_factory, ...)
|
30
|
+
name, factory = RSpec.normalize_name_and_factory(name_and_factory)
|
31
|
+
|
32
|
+
let(name) { FactoryBot.create(factory, ...) }
|
33
|
+
end
|
34
|
+
|
35
|
+
def build(name_and_factory, ...)
|
36
|
+
name, factory = RSpec.normalize_name_and_factory(name_and_factory)
|
37
|
+
|
38
|
+
let!(name) { FactoryBot.build(factory, ...) }
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_lazy(name_and_factory, ...)
|
42
|
+
name, factory = RSpec.normalize_name_and_factory(name_and_factory)
|
43
|
+
|
44
|
+
let(name) { FactoryBot.build(factory, ...) }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'refinements/version'
|
2
|
+
|
3
|
+
module FactoryBot
|
4
|
+
module Refinements
|
5
|
+
begin
|
6
|
+
require 'active_record'
|
7
|
+
rescue LoadError
|
8
|
+
# optional
|
9
|
+
else
|
10
|
+
require_relative 'refinements/active_record'
|
11
|
+
|
12
|
+
include ActiveRecord
|
13
|
+
end
|
14
|
+
|
15
|
+
begin
|
16
|
+
require 'rspec/core'
|
17
|
+
rescue LoadError
|
18
|
+
# optional
|
19
|
+
else
|
20
|
+
require_relative 'refinements/rspec'
|
21
|
+
|
22
|
+
include RSpec
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'factory_bot/refinements'
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: factory_bot-refinements
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Keita Urashima
|
8
|
+
- Ryunosuke Sato
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2021-11-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: factory_bot
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
description:
|
29
|
+
email:
|
30
|
+
- ursm@ursm.jp
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- README.md
|
36
|
+
- lib/factory_bot-refinements.rb
|
37
|
+
- lib/factory_bot/refinements.rb
|
38
|
+
- lib/factory_bot/refinements/active_record.rb
|
39
|
+
- lib/factory_bot/refinements/rspec.rb
|
40
|
+
- lib/factory_bot/refinements/version.rb
|
41
|
+
homepage: https://github.com/ursm/factory_bot-refinements
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata:
|
45
|
+
homepage_uri: https://github.com/ursm/factory_bot-refinements
|
46
|
+
source_code_uri: https://github.com/ursm/factory_bot-refinements.git
|
47
|
+
changelog_uri: https://github.com/ursm/factory_bot-refinements/releases
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '2.7'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.2.31
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Extensions for factory_bot
|
67
|
+
test_files: []
|