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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bf4172edafa15e0c0a33f21c7b468a9dc0b174b7
4
- data.tar.gz: 9e96fe2c40e58ace973698c3f59ac8cc3822a077
3
+ metadata.gz: aaa3e7a3fadb7a11032d347388fb0ee8e2bc51fc
4
+ data.tar.gz: b82cd8864420212b0ec69ed10dcee10553ebb468
5
5
  SHA512:
6
- metadata.gz: 53c4e36450a96f6acda28bced824e382a8c45f706d1a83245c2773f0b67e1a59988f25a85392f9822b4074354102a4329b39522cfcb6c7ef1d89edc13634cacf
7
- data.tar.gz: 239e928ca1e1a2b3f96be3045a27df12eabce2d385bb942a489f23e0c38c862f55516ac07f5de696cd5d175b843d17c639708992f0b291edb18458353995c69f
6
+ metadata.gz: 60f13c2519e2a42553657c65190a2bc7dbaf9494839d270b1ffa9f07190f9f7aedbb634895545764440a330ed14af35e987198dc7f45c44bc089bd09e0a59b74
7
+ data.tar.gz: af6f469463db9c2babb7b038418b9d85ada22d0af24ff66ca95d0628306355486d1cab8405c8f2f6f00aca3bd9c9278a7c84f077f8ee1a4dafe5ddc937584199
@@ -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 the rails application, the gem will do it automatically.
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
- The way you define your fabricators should be very familiar already.
49
- Fabricators.define do
50
- fabricator :user do
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
- == Inehritance
52
+ == Inheritance
56
53
 
57
- Can be declare nested or separate:
58
- Fabricators.define do
59
- fabricator :user do
60
- name 'example'
61
- fabricator :user_with_email do
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
- == Generators
71
-
72
- Define them outside your fabricators and use them as attributes:
73
- Fabricators.define do
74
- generator(:name) { 'example' }
75
- generator(:email) { |n| "example#{n}@mail.com" }
76
- generator(:age)
77
- fabricator :user do
78
- name
79
- email
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
- Fabricators.define do
88
- fabricator :user do
89
- posts
90
- comments 4 # By default 1, but can be overwritten
91
- end
92
- fabricator :post do
93
- user
94
- end
95
- fabricator :comment do
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
- Fabricators.define do
104
- generator(:title, aliases: [:first_name, :last_name]) { 'example' }
105
- fabricators :user, aliases: :author do
106
- first_name
107
- last_name
108
- comments
109
- end
110
- fabricators :post, aliases: :comment do
111
- title
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
- Fabricators.define do
120
- fabricators :user do
121
- name 'example'
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
- Fabricators.define do
130
- before(:build) { |u| u.name = 'global' }
131
- fabricator :user do
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
@@ -1,7 +1,7 @@
1
1
  require 'fabricators/callbacks'
2
2
  require 'fabricators/definitions'
3
3
  require 'fabricators/fabricator'
4
- require 'fabricators/generator'
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 definitions
19
- @definitions ||= Definitions.new
18
+ def clean
19
+ records.pop.destroy until records.empty?
20
20
  end
21
-
22
- def generate(name)
23
- definitions.find(name, :generator).generate
21
+
22
+ def records
23
+ @records ||= []
24
24
  end
25
25
 
26
- def paths
27
- @paths ||= %w(test/fabricators spec/fabricators).map { |path| Rails.root.join(path) }
26
+ def definitions
27
+ @definitions ||= Definitions.new
28
28
  end
29
29
 
30
30
  def path
31
- @path ||= paths.find { |path| Dir.exists? 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 populate
35
- Dir[path.join('**', '*.rb')].each do |file|
36
- load file
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
 
@@ -1,8 +1,8 @@
1
1
  module Fabricators
2
- class Generator
2
+ class Attribute
3
3
 
4
- def initialize(starts_at, &block)
5
- @index = starts_at
4
+ def initialize(&block)
5
+ @index = 0
6
6
  @block = block
7
7
  end
8
8
 
@@ -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
- @generators = {}
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 generator(name, first=0, options={}, &block)
23
- generator = Generator.new(first, &block)
22
+ def attribute(name, options={}, &block)
23
+ attribute = Attribute.new(&block)
24
24
  iterate_names name, options do |name|
25
- @generators[name] = generator
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 :generator
34
- @generators[name]
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
@@ -79,7 +79,9 @@ module Fabricators
79
79
  def create_one(options={})
80
80
  build_one(options).tap do |instance|
81
81
  trigger :before_create, instance
82
- instance.save
82
+ if instance.save
83
+ Fabricators.records << instance
84
+ end
83
85
  trigger :after_create, instance
84
86
  end
85
87
  end
@@ -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 generator = Fabricators.definitions.find(name, :generator) rescue nil
33
- logic = -> { generator.generate }
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
@@ -2,12 +2,31 @@ module Fabricators
2
2
  class Railtie < Rails::Railtie
3
3
 
4
4
  initializer 'fabricators' do
5
- test_framework = config.app_generators.options[:rails][:test_framework]
6
- config.app_generators.test_framework test_framework, fixture: false, fixture_replacement: :fabricators
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.populate
29
+ Fabricators.load_files
11
30
  end
12
31
 
13
32
  end
@@ -1,5 +1,5 @@
1
1
  module Fabricators
2
2
 
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.4'
4
4
 
5
5
  end
@@ -1,4 +1,2 @@
1
- Fabricators.define do
2
- fabricator :<%= singular_table_name %> do
3
- end
1
+ fabricator :<%= singular_table_name %> do
4
2
  end
@@ -4,16 +4,17 @@ class AliasesTest < ActiveSupport::TestCase
4
4
 
5
5
  setup do
6
6
  Fabricators.define do
7
- generator :mobile, 0, aliases: :phone
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::Generator, Fabricators.definitions.find(:mobile, :generator)
16
- assert_kind_of Fabricators::Generator, Fabricators.definitions.find(:phone, :generator)
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
@@ -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
@@ -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