factory_girl-seeds 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +31 -0
- data/factory_girl-seeds.gemspec +1 -1
- data/lib/factory_girl-seeds/seeds.rb +7 -7
- data/lib/factory_girl-seeds/version.rb +1 -1
- data/spec/factory_girl-seeds/seeds_spec.rb +3 -3
- data/spec/spec_helper.rb +4 -5
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce9dccfd3b6fb6d09b045d10b868584da7447481
|
4
|
+
data.tar.gz: 0fa3661dca9d877b3bc18d03b39b79f630f04d0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc383ca57ff507c69096496d326aebd056e46f9808c7c7e33579d7ca4e9b00aab0735281d52487e5dbf7cc3e577ef400629e9e4b490774be6a873b22d20c2f93
|
7
|
+
data.tar.gz: 67a4b14db21c1e844ec08eb14672c833d2a1238a6bd60b837a6f4ab4d14b08a7539f80577307fb04246bd82f6c750d0bf8d09d9b30b681fd98ed7efa41d4c4a5
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/evrone/factory_girl-seeds.svg?branch=master)](https://travis-ci.org/evrone/factory_girl-seeds)
|
1
2
|
[![Code Climate](https://codeclimate.com/github/evrone/factory_girl-seeds.png)](https://codeclimate.com/github/evrone/factory_girl-seeds)
|
2
3
|
|
3
4
|
# FactoryGirl Seeds
|
@@ -82,6 +83,36 @@ Short DSL also available:
|
|
82
83
|
user = seed(:user)
|
83
84
|
```
|
84
85
|
|
86
|
+
### 4. Using Factory Girl traits
|
87
|
+
|
88
|
+
You can create models via factories and traits (like `create(:user, admin)`), but you *can not* obtain it with the `seed(:user, :admin)`. To be able to obtain a record it is recommended to define specific factories with a set of traits and unique names just like in the example in [Getting stated](https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#traits) guide:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
factory :user do
|
92
|
+
name "Friendly User"
|
93
|
+
login { name }
|
94
|
+
|
95
|
+
trait :male do
|
96
|
+
name "John Doe"
|
97
|
+
gender "Male"
|
98
|
+
end
|
99
|
+
|
100
|
+
trait :female do
|
101
|
+
name "Jane Doe"
|
102
|
+
gender "Female"
|
103
|
+
end
|
104
|
+
|
105
|
+
trait :admin do
|
106
|
+
admin true
|
107
|
+
end
|
108
|
+
|
109
|
+
factory :male_admin, traits: [:male, :admin]
|
110
|
+
factory :female_admin, traits: [:admin, :female]
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
When factories declared in this manner, you can obtain a record with `seed(:male_admin)`
|
115
|
+
|
85
116
|
## How it works?
|
86
117
|
|
87
118
|
```FactoryGirl::SeedGenerator.create``` method creates record in DB before transaction begins. Then ```it``` block starts transaction so when you update record returned by ```FactoryGirl.seed``` it is wrapped in transaction. This guarantees that every ```it``` block works with clean record.
|
data/factory_girl-seeds.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
20
|
gem.require_paths = ["lib"]
|
21
21
|
|
22
|
-
gem.add_dependency "
|
22
|
+
gem.add_dependency "factory_bot"
|
23
23
|
|
24
24
|
gem.add_development_dependency "rake"
|
25
25
|
gem.add_development_dependency "rspec"
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'factory_bot'
|
2
2
|
|
3
3
|
module FactoryGirl
|
4
4
|
class SeedGenerator
|
@@ -6,7 +6,7 @@ module FactoryGirl
|
|
6
6
|
@classes = {}
|
7
7
|
|
8
8
|
def self.create(factory_name, *attributes)
|
9
|
-
model =
|
9
|
+
model = FactoryBot.create(factory_name, *attributes)
|
10
10
|
@ids[factory_name] = model.id
|
11
11
|
@classes[factory_name] = model.class
|
12
12
|
|
@@ -18,7 +18,7 @@ module FactoryGirl
|
|
18
18
|
|
19
19
|
if seed_id
|
20
20
|
seed_class = @classes[factory_name]
|
21
|
-
seed_class.where(
|
21
|
+
seed_class.where(seed_class.primary_key => seed_id).first || create(factory_name)
|
22
22
|
else
|
23
23
|
create(factory_name)
|
24
24
|
end
|
@@ -31,7 +31,7 @@ module FactoryGirl
|
|
31
31
|
module SeedMethods
|
32
32
|
def seed(factory_name)
|
33
33
|
if defined?(Rails) && !Rails.env.test?
|
34
|
-
|
34
|
+
FactoryBot.create(factory_name)
|
35
35
|
else
|
36
36
|
FactoryGirl::SeedGenerator[factory_name]
|
37
37
|
end
|
@@ -40,6 +40,6 @@ module FactoryGirl
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
FactoryBot::Syntax::Methods.send(:include, FactoryGirl::Syntax::SeedMethods)
|
44
|
+
FactoryBot::SyntaxRunner.send(:include, FactoryGirl::Syntax::SeedMethods)
|
45
|
+
FactoryBot.send(:extend, FactoryGirl::Syntax::SeedMethods)
|
@@ -21,15 +21,15 @@ describe FactoryGirl::SeedGenerator do
|
|
21
21
|
it "seeds with attributes and traits" do
|
22
22
|
seed = klass.create(:user, :david, nickname: 'Goliath')
|
23
23
|
expect(seed.name).to eql 'David'
|
24
|
-
expect(seed.
|
24
|
+
expect(seed.nickname).to eql 'Goliath'
|
25
25
|
end
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
describe
|
29
|
+
describe FactoryBot do
|
30
30
|
describe ".seed" do
|
31
31
|
it do
|
32
|
-
expect(
|
32
|
+
expect(FactoryBot.seed(:user)).to be
|
33
33
|
end
|
34
34
|
end
|
35
35
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
|
-
require '
|
1
|
+
require 'factory_bot'
|
2
2
|
require 'active_record'
|
3
3
|
require 'factory_girl-seeds'
|
4
4
|
|
5
|
-
ActiveRecord::Base.
|
6
|
-
ActiveRecord::Base.establish_connection('test')
|
5
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
7
6
|
|
8
7
|
ActiveRecord::Migration.verbose = false
|
9
8
|
ActiveRecord::Schema.define(version: 0) do
|
@@ -15,7 +14,7 @@ end
|
|
15
14
|
|
16
15
|
class User < ActiveRecord::Base;end
|
17
16
|
|
18
|
-
|
17
|
+
FactoryBot.define do
|
19
18
|
factory :user do
|
20
19
|
name "AB"
|
21
20
|
|
@@ -26,5 +25,5 @@ FactoryGirl.define do
|
|
26
25
|
end
|
27
26
|
|
28
27
|
RSpec.configure do |config|
|
29
|
-
config.include
|
28
|
+
config.include FactoryBot::Syntax::Methods
|
30
29
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_girl-seeds
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Balashov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: factory_bot
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -88,6 +88,7 @@ extensions: []
|
|
88
88
|
extra_rdoc_files: []
|
89
89
|
files:
|
90
90
|
- ".gitignore"
|
91
|
+
- CHANGELOG.md
|
91
92
|
- Gemfile
|
92
93
|
- LICENSE.txt
|
93
94
|
- README.md
|