-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.md.hms
105 lines (70 loc) · 2.71 KB
/
README.md.hms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
--- context ---
summary: ruby -e "puts Gem::Specification.load('factory_bot-refinements.gemspec').summary"
--- template ---
# FactoryBot::Refinements
{{summary}}
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'factory_bot-refinements'
```
And then execute:
$ bundle install
Or install it yourself as:
$ gem install factory_bot-refinements
## Usage
Add the following code to your test file:
``` ruby
# ActiveRecord extensions
using FactoryBot::Refinements::ActiveRecord
# RSpec extensions
using FactoryBot::Refinements::RSpec
# Or both of them
using FactoryBot::Refinements
```
### FactoryBot::Refinements::ActiveRecord
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.
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()`.
``` ruby
using FactoryBot::Refinements::ActiveRecord
describe MyApp do
example do
user = create(:user)
user.posts.create_with_factory(:published, title: 'hello')
# Equivalent to:
# FactoryBot.create(:post, :published, user: user, title: 'hello')
user.posts.factory(:published_post).create_with_factory
# Equivalent to:
# FactoryBot.create(:published_post, user: user)
user.posts.build_with_factory
# Equivalent to:
# FactoryBot.build(:post, user: user)
end
end
```
### FactoryBot::Refinements::RSpec
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.
``` ruby
using FactoryBot::Refinements::RSpec
describe MyApp do
create :user, name: 'alice'
# Equivalent to:
# let!(:user) { FactoryBot.create(:user, name: 'alice') }
create :user.as(:bob), :admin, name: 'bob'
# Equivalent to:
# let!(:bob) { FactoryBot.create(:user, :admin, name: 'bob') }
create_lazy :user
# Equivalent to (let, not let!):
# let(:user) { FactoryBot.create(:user) }
build :user
# Equivalent to:
# let!(:user) { FactoryBot.build(:user) }
build_lazy :user
# Equivalent to:
# let(:user) { FactoryBot.build(:user) }
end
```
#### Limitations
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).
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).