fabricators 0.0.1 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +56 -71
- data/lib/fabricators.rb +14 -12
- data/lib/fabricators/{generator.rb → attribute.rb} +3 -3
- data/lib/fabricators/definitions.rb +8 -8
- data/lib/fabricators/fabricator.rb +3 -1
- data/lib/fabricators/proxy.rb +2 -2
- data/lib/fabricators/railtie.rb +22 -3
- data/lib/fabricators/version.rb +1 -1
- data/lib/generators/fabricators/model/templates/fabricator.rb +1 -3
- data/test/aliases_test.rb +4 -3
- data/test/associations_test.rb +2 -2
- data/test/attributes_test.rb +36 -0
- data/test/clean_test.rb +18 -0
- data/test/dummy/log/test.log +5329 -0
- data/test/dummy/test/fabricators/users.rb +2 -4
- data/test/generators_test.rb +7 -30
- data/test/load_test.rb +1 -1
- data/test/merges_test.rb +3 -3
- data/test/test_helper.rb +1 -2
- metadata +7 -7
- data/test/dummy/test/fabricators/people.rb +0 -4
- data/test/fixtures_test.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aaa3e7a3fadb7a11032d347388fb0ee8e2bc51fc
|
4
|
+
data.tar.gz: b82cd8864420212b0ec69ed10dcee10553ebb468
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 60f13c2519e2a42553657c65190a2bc7dbaf9494839d270b1ffa9f07190f9f7aedbb634895545764440a330ed14af35e987198dc7f45c44bc089bd09e0a59b74
|
7
|
+
data.tar.gz: af6f469463db9c2babb7b038418b9d85ada22d0af24ff66ca95d0628306355486d1cab8405c8f2f6f00aca3bd9c9278a7c84f077f8ee1a4dafe5ddc937584199
|
data/README.rdoc
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
=== NOTE: From version 0.0.2 the syntax has been simplified.
|
2
|
+
|
3
|
+
---
|
4
|
+
|
1
5
|
{<img src="https://badge.fury.io/rb/fabricators.png" alt="Gem Version" />}[http://badge.fury.io/rb/fabricators] {<img src="https://codeclimate.com/github/museways/fabricators.png" />}[https://codeclimate.com/github/museways/fabricators] {<img src="https://travis-ci.org/museways/fabricators.png?branch=master" alt="Build Status" />}[https://travis-ci.org/museways/fabricators] {<img src="https://gemnasium.com/museways/fabricators.png" alt="Dependency Status" />}[https://gemnasium.com/museways/fabricators]
|
2
6
|
|
3
7
|
= Fabricators
|
@@ -14,23 +18,18 @@ Then bundle:
|
|
14
18
|
|
15
19
|
= Configuration
|
16
20
|
|
17
|
-
There is no need to configure
|
21
|
+
There is no need to configure anything, all is done automatically:
|
22
|
+
|
23
|
+
- Loading the definitions.
|
24
|
+
- Replacing the fixtures generators.
|
25
|
+
- Including the methods inside your testing framework.
|
26
|
+
- Cleaning the database after each test.
|
18
27
|
|
19
28
|
= Usage
|
20
29
|
|
21
30
|
== Methods
|
22
31
|
|
23
32
|
There are three methods available:
|
24
|
-
Fabricators.attributes_for
|
25
|
-
Fabricators.build
|
26
|
-
Fabricators.create
|
27
|
-
|
28
|
-
To not write "Fabricators" every time you can include the methods in your tests:
|
29
|
-
class ActiveSupport::TestCase
|
30
|
-
include Fabricators::Methods
|
31
|
-
end
|
32
|
-
|
33
|
-
Then you can you just:
|
34
33
|
attributes_for
|
35
34
|
build
|
36
35
|
create
|
@@ -45,90 +44,76 @@ To create lists just pass the desired size as second parameter to build and crea
|
|
45
44
|
|
46
45
|
== Fabricators
|
47
46
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
name 'example'
|
52
|
-
end
|
47
|
+
Define them inside test/fabricators or spec/fabricators:
|
48
|
+
fabricator :user do
|
49
|
+
name 'example'
|
53
50
|
end
|
54
51
|
|
55
|
-
==
|
52
|
+
== Inheritance
|
56
53
|
|
57
|
-
Can be declare nested or
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
email 'example@mail.com'
|
63
|
-
end
|
64
|
-
end
|
65
|
-
fabricator :user_with_age, parent: :user do
|
66
|
-
age 9
|
54
|
+
Can be declare nested or separated:
|
55
|
+
fabricator :user do
|
56
|
+
name 'example'
|
57
|
+
fabricator :user_with_email do
|
58
|
+
email 'example@mail.com'
|
67
59
|
end
|
68
60
|
end
|
61
|
+
fabricator :user_with_age, parent: :user do
|
62
|
+
age 9
|
63
|
+
end
|
69
64
|
|
70
|
-
==
|
71
|
-
|
72
|
-
Define them outside your fabricators and
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
age
|
81
|
-
end
|
65
|
+
== Attributes
|
66
|
+
|
67
|
+
Define them outside your fabricators and reference them by name:
|
68
|
+
attribute(:name) { 'example' }
|
69
|
+
attribute(:email) { |n| "example#{n}@mail.com" }
|
70
|
+
attribute(:age)
|
71
|
+
fabricator :user do
|
72
|
+
name
|
73
|
+
email
|
74
|
+
age
|
82
75
|
end
|
83
76
|
|
84
77
|
== Associations
|
85
78
|
|
86
79
|
Associations are declared just by the name of the association:
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
user
|
97
|
-
end
|
80
|
+
fabricator :user do
|
81
|
+
posts
|
82
|
+
comments 4 # By default 1, but can be overwritten
|
83
|
+
end
|
84
|
+
fabricator :post do
|
85
|
+
user
|
86
|
+
end
|
87
|
+
fabricator :comment do
|
88
|
+
user
|
98
89
|
end
|
99
90
|
|
100
91
|
== Aliases
|
101
92
|
|
102
93
|
The aliases are essential when there is the need of context:
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
author
|
113
|
-
end
|
94
|
+
attribute(:title, aliases: [:first_name, :last_name]) { 'example' }
|
95
|
+
fabricators :user, aliases: :author do
|
96
|
+
first_name
|
97
|
+
last_name
|
98
|
+
comments
|
99
|
+
end
|
100
|
+
fabricators :post, aliases: :comment do
|
101
|
+
title
|
102
|
+
author
|
114
103
|
end
|
115
104
|
|
116
105
|
== Dependent attributes
|
117
106
|
|
118
107
|
If you need to use some logic that depends of another attribute you can go:
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
email { "#{name}@mail.com" }
|
123
|
-
end
|
108
|
+
fabricators :user do
|
109
|
+
name 'example'
|
110
|
+
email { "#{name}@mail.com" }
|
124
111
|
end
|
125
112
|
|
126
113
|
== Callbacks
|
127
114
|
|
128
115
|
The available callbacks are before(:build), before(:create), after(:build) and after(:create):
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
after(:build) { |u| u.name = 'local' }
|
133
|
-
end
|
116
|
+
before(:build) { |u| u.name = 'global' }
|
117
|
+
fabricator :user do
|
118
|
+
after(:build) { |u| u.name = 'local' }
|
134
119
|
end
|
data/lib/fabricators.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'fabricators/callbacks'
|
2
2
|
require 'fabricators/definitions'
|
3
3
|
require 'fabricators/fabricator'
|
4
|
-
require 'fabricators/
|
4
|
+
require 'fabricators/attribute'
|
5
5
|
require 'fabricators/reader'
|
6
6
|
require 'fabricators/proxy'
|
7
7
|
require 'fabricators/methods'
|
@@ -15,25 +15,27 @@ module Fabricators
|
|
15
15
|
definitions.instance_eval &block
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
19
|
-
|
18
|
+
def clean
|
19
|
+
records.pop.destroy until records.empty?
|
20
20
|
end
|
21
|
-
|
22
|
-
def
|
23
|
-
|
21
|
+
|
22
|
+
def records
|
23
|
+
@records ||= []
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
27
|
-
@
|
26
|
+
def definitions
|
27
|
+
@definitions ||= Definitions.new
|
28
28
|
end
|
29
29
|
|
30
30
|
def path
|
31
|
-
@path ||=
|
31
|
+
@path ||= %w(test spec).map{ |dir| Rails.root.join(dir) }.find{ |path| Dir.exist?(path) }.try(:join, 'fabricators')
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
34
|
+
def load_files
|
35
|
+
if path
|
36
|
+
Dir[path.join('**', '*.rb')].each do |file|
|
37
|
+
Fabricators.definitions.instance_eval File.read(file)
|
38
|
+
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
@@ -3,12 +3,12 @@ module Fabricators
|
|
3
3
|
include Callbacks
|
4
4
|
|
5
5
|
def initialize
|
6
|
-
reset
|
6
|
+
reset
|
7
7
|
end
|
8
8
|
|
9
|
-
def reset
|
9
|
+
def reset
|
10
10
|
@fabricators = {}
|
11
|
-
@
|
11
|
+
@attributes = {}
|
12
12
|
@callbacks = {}
|
13
13
|
end
|
14
14
|
|
@@ -19,10 +19,10 @@ module Fabricators
|
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
|
22
|
+
def attribute(name, options={}, &block)
|
23
|
+
attribute = Attribute.new(&block)
|
24
24
|
iterate_names name, options do |name|
|
25
|
-
@
|
25
|
+
@attributes[name] = attribute
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -30,8 +30,8 @@ module Fabricators
|
|
30
30
|
case type
|
31
31
|
when :fabricator
|
32
32
|
@fabricators[name]
|
33
|
-
when :
|
34
|
-
@
|
33
|
+
when :attribute
|
34
|
+
@attributes[name]
|
35
35
|
end.tap do |definition|
|
36
36
|
raise "Definition #{name} of type #{type} not found" unless definition
|
37
37
|
end
|
data/lib/fabricators/proxy.rb
CHANGED
@@ -29,8 +29,8 @@ module Fabricators
|
|
29
29
|
logic = -> { fabricator.send(strategy, options) }
|
30
30
|
elsif fabricator = Fabricators.definitions.find(name.to_s.singularize.to_sym, :fabricator) rescue nil
|
31
31
|
logic = -> { fabricator.send(strategy, (args.first || 1), options) }
|
32
|
-
elsif
|
33
|
-
logic = -> {
|
32
|
+
elsif attribute = Fabricators.definitions.find(name, :attribute) rescue nil
|
33
|
+
logic = -> { attribute.generate }
|
34
34
|
elsif args.any?
|
35
35
|
logic = -> { args.first }
|
36
36
|
end
|
data/lib/fabricators/railtie.rb
CHANGED
@@ -2,12 +2,31 @@ module Fabricators
|
|
2
2
|
class Railtie < Rails::Railtie
|
3
3
|
|
4
4
|
initializer 'fabricators' do
|
5
|
-
|
6
|
-
|
5
|
+
config.app_generators.test_framework(
|
6
|
+
config.app_generators.options[:rails][:test_framework],
|
7
|
+
fixture: false,
|
8
|
+
fixture_replacement: :fabricators
|
9
|
+
)
|
10
|
+
if defined? RSpec
|
11
|
+
require 'rspec/rails'
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.include Fabricators::Methods
|
14
|
+
config.after(:each) do
|
15
|
+
Fabricators.clean
|
16
|
+
end
|
17
|
+
end
|
18
|
+
else
|
19
|
+
class ActiveSupport::TestCase
|
20
|
+
include Fabricators::Methods
|
21
|
+
teardown do
|
22
|
+
Fabricators.clean
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
7
26
|
end
|
8
27
|
|
9
28
|
config.after_initialize do
|
10
|
-
Fabricators.
|
29
|
+
Fabricators.load_files
|
11
30
|
end
|
12
31
|
|
13
32
|
end
|
data/lib/fabricators/version.rb
CHANGED
data/test/aliases_test.rb
CHANGED
@@ -4,16 +4,17 @@ class AliasesTest < ActiveSupport::TestCase
|
|
4
4
|
|
5
5
|
setup do
|
6
6
|
Fabricators.define do
|
7
|
-
|
7
|
+
attribute(:mobile, aliases: :phone)
|
8
8
|
fabricator :user, aliases: [:owner, :author] do
|
9
9
|
name 'name'
|
10
|
+
phone
|
10
11
|
end
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
14
15
|
test "generator aliases" do
|
15
|
-
assert_kind_of Fabricators::
|
16
|
-
assert_kind_of Fabricators::
|
16
|
+
assert_kind_of Fabricators::Attribute, Fabricators.definitions.find(:mobile, :attribute)
|
17
|
+
assert_kind_of Fabricators::Attribute, Fabricators.definitions.find(:phone, :attribute)
|
17
18
|
end
|
18
19
|
|
19
20
|
test "fabricator aliases" do
|
data/test/associations_test.rb
CHANGED
@@ -27,7 +27,7 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
27
27
|
user = build(:post_with_built_user).user
|
28
28
|
assert_kind_of User, user
|
29
29
|
assert user.new_record?
|
30
|
-
|
30
|
+
|
31
31
|
user = build(:post_with_created_user).user
|
32
32
|
assert_kind_of User, user
|
33
33
|
assert user.persisted?
|
@@ -39,7 +39,7 @@ class AssociationsTest < ActiveSupport::TestCase
|
|
39
39
|
post = posts.first
|
40
40
|
assert_kind_of Post, post
|
41
41
|
assert post.new_record?
|
42
|
-
|
42
|
+
|
43
43
|
posts = build(:user_with_created_posts).posts
|
44
44
|
assert_equal 2, posts.size
|
45
45
|
post = posts.first
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class AttributesTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Fabricators.define do
|
7
|
+
attribute(:name) { 'name' }
|
8
|
+
attribute(:email) { |n| "mail#{n}@example.com" }
|
9
|
+
attribute(:age)
|
10
|
+
fabricator :user do
|
11
|
+
name
|
12
|
+
age
|
13
|
+
email
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
test "static generator" do
|
19
|
+
assert_equal 'name', attributes_for(:user)[:name]
|
20
|
+
assert_equal 'name', build(:user).name
|
21
|
+
assert_equal 'name', create(:user).name
|
22
|
+
end
|
23
|
+
|
24
|
+
test "block generator" do
|
25
|
+
assert_equal 'mail1@example.com', attributes_for(:user)[:email]
|
26
|
+
assert_equal 'mail2@example.com', build(:user).email
|
27
|
+
assert_equal 'mail3@example.com', create(:user).email
|
28
|
+
end
|
29
|
+
|
30
|
+
test "number generator" do
|
31
|
+
assert_equal 1, attributes_for(:user)[:age]
|
32
|
+
assert_equal 2, build(:user).age
|
33
|
+
assert_equal 3, create(:user).age
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/test/clean_test.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CleanTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
setup do
|
6
|
+
Fabricators.define do
|
7
|
+
fabricator :user do
|
8
|
+
end
|
9
|
+
end
|
10
|
+
create :user, 5
|
11
|
+
Fabricators.clean
|
12
|
+
end
|
13
|
+
|
14
|
+
test "clean records created" do
|
15
|
+
assert_equal 0, User.count
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|