factory_girl_kibiz0r 2.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data/Appraisals +12 -0
- data/CONTRIBUTION_GUIDELINES.md +9 -0
- data/Changelog +29 -0
- data/GETTING_STARTED.md +246 -0
- data/Gemfile +12 -0
- data/Gemfile.lock +62 -0
- data/LICENSE +19 -0
- data/README.md +64 -0
- data/Rakefile +54 -0
- data/features/factory_girl_steps.feature +151 -0
- data/features/step_definitions/database_steps.rb +20 -0
- data/features/support/env.rb +6 -0
- data/features/support/factories.rb +84 -0
- data/lib/factory_girl/aliases.rb +21 -0
- data/lib/factory_girl/attribute/association.rb +24 -0
- data/lib/factory_girl/attribute/callback.rb +16 -0
- data/lib/factory_girl/attribute/dynamic.rb +21 -0
- data/lib/factory_girl/attribute/implicit.rb +36 -0
- data/lib/factory_girl/attribute/list.rb +19 -0
- data/lib/factory_girl/attribute/sequence.rb +16 -0
- data/lib/factory_girl/attribute/static.rb +18 -0
- data/lib/factory_girl/attribute.rb +42 -0
- data/lib/factory_girl/definition_proxy.rb +147 -0
- data/lib/factory_girl/deprecated.rb +18 -0
- data/lib/factory_girl/factory.rb +196 -0
- data/lib/factory_girl/find_definitions.rb +23 -0
- data/lib/factory_girl/proxy/attributes_for.rb +21 -0
- data/lib/factory_girl/proxy/build.rb +36 -0
- data/lib/factory_girl/proxy/create.rb +16 -0
- data/lib/factory_girl/proxy/stub.rb +64 -0
- data/lib/factory_girl/proxy.rb +87 -0
- data/lib/factory_girl/rails2.rb +1 -0
- data/lib/factory_girl/registry.rb +45 -0
- data/lib/factory_girl/sequence.rb +31 -0
- data/lib/factory_girl/step_definitions.rb +60 -0
- data/lib/factory_girl/syntax/blueprint.rb +42 -0
- data/lib/factory_girl/syntax/default.rb +33 -0
- data/lib/factory_girl/syntax/generate.rb +73 -0
- data/lib/factory_girl/syntax/make.rb +41 -0
- data/lib/factory_girl/syntax/methods.rb +86 -0
- data/lib/factory_girl/syntax/sham.rb +45 -0
- data/lib/factory_girl/syntax/vintage.rb +152 -0
- data/lib/factory_girl/syntax.rb +12 -0
- data/lib/factory_girl/version.rb +4 -0
- data/lib/factory_girl.rb +54 -0
- data/spec/acceptance/acceptance_helper.rb +11 -0
- data/spec/acceptance/attribute_aliases_spec.rb +26 -0
- data/spec/acceptance/attributes_for_spec.rb +48 -0
- data/spec/acceptance/build_spec.rb +35 -0
- data/spec/acceptance/build_stubbed_spec.rb +79 -0
- data/spec/acceptance/callbacks_spec.rb +53 -0
- data/spec/acceptance/create_spec.rb +67 -0
- data/spec/acceptance/default_strategy_spec.rb +27 -0
- data/spec/acceptance/definition_spec.rb +28 -0
- data/spec/acceptance/parent_spec.rb +39 -0
- data/spec/acceptance/sequence_spec.rb +34 -0
- data/spec/acceptance/syntax/blueprint_spec.rb +32 -0
- data/spec/acceptance/syntax/generate_spec.rb +60 -0
- data/spec/acceptance/syntax/make_spec.rb +35 -0
- data/spec/acceptance/syntax/sham_spec.rb +44 -0
- data/spec/acceptance/syntax/vintage_spec.rb +224 -0
- data/spec/factory_girl/aliases_spec.rb +33 -0
- data/spec/factory_girl/attribute/association_spec.rb +33 -0
- data/spec/factory_girl/attribute/callback_spec.rb +23 -0
- data/spec/factory_girl/attribute/dynamic_spec.rb +60 -0
- data/spec/factory_girl/attribute/implicit_spec.rb +50 -0
- data/spec/factory_girl/attribute/sequence_spec.rb +21 -0
- data/spec/factory_girl/attribute/static_spec.rb +29 -0
- data/spec/factory_girl/attribute_spec.rb +39 -0
- data/spec/factory_girl/definition_proxy_spec.rb +129 -0
- data/spec/factory_girl/deprecated_spec.rb +66 -0
- data/spec/factory_girl/factory_spec.rb +374 -0
- data/spec/factory_girl/find_definitions_spec.rb +100 -0
- data/spec/factory_girl/proxy/attributes_for_spec.rb +52 -0
- data/spec/factory_girl/proxy/build_spec.rb +86 -0
- data/spec/factory_girl/proxy/create_spec.rb +107 -0
- data/spec/factory_girl/proxy/stub_spec.rb +80 -0
- data/spec/factory_girl/proxy_spec.rb +102 -0
- data/spec/factory_girl/registry_spec.rb +83 -0
- data/spec/factory_girl/sequence_spec.rb +96 -0
- data/spec/factory_girl_spec.rb +17 -0
- data/spec/spec_helper.rb +93 -0
- metadata +294 -0
@@ -0,0 +1,152 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
module Syntax
|
3
|
+
module Vintage
|
4
|
+
module Factory
|
5
|
+
# Defines a new factory that can be used by the build strategies (create and
|
6
|
+
# build) to build new objects.
|
7
|
+
#
|
8
|
+
# Arguments:
|
9
|
+
# * name: +Symbol+ or +String+
|
10
|
+
# A unique name used to identify this factory.
|
11
|
+
# * options: +Hash+
|
12
|
+
#
|
13
|
+
# Options:
|
14
|
+
# * class: +Symbol+, +Class+, or +String+
|
15
|
+
# The class that will be used when generating instances for this factory. If not specified, the class will be guessed from the factory name.
|
16
|
+
# * parent: +Symbol+
|
17
|
+
# The parent factory. If specified, the attributes from the parent
|
18
|
+
# factory will be copied to the current one with an ability to override
|
19
|
+
# them.
|
20
|
+
# * default_strategy: +Symbol+
|
21
|
+
# DEPRECATED.
|
22
|
+
# The strategy that will be used by the Factory shortcut method.
|
23
|
+
# Defaults to :create.
|
24
|
+
#
|
25
|
+
# Yields: +Factory+
|
26
|
+
# The newly created factory.
|
27
|
+
def self.define(name, options = {})
|
28
|
+
factory = FactoryGirl::Factory.new(name, options)
|
29
|
+
proxy = FactoryGirl::DefinitionProxy.new(factory)
|
30
|
+
yield(proxy)
|
31
|
+
if parent = options.delete(:parent)
|
32
|
+
factory.inherit_from(FactoryGirl.factory_by_name(parent))
|
33
|
+
end
|
34
|
+
FactoryGirl.register_factory(factory)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Executes the default strategy for the given factory. This is usually create,
|
38
|
+
# but it can be overridden for each factory.
|
39
|
+
#
|
40
|
+
# DEPRECATED
|
41
|
+
#
|
42
|
+
# Use create instead.
|
43
|
+
#
|
44
|
+
# Arguments:
|
45
|
+
# * name: +Symbol+ or +String+
|
46
|
+
# The name of the factory that should be used.
|
47
|
+
# * overrides: +Hash+
|
48
|
+
# Attributes to overwrite for this instance.
|
49
|
+
#
|
50
|
+
# Returns: +Object+
|
51
|
+
# The result of the default strategy.
|
52
|
+
def self.default_strategy(name, overrides = {})
|
53
|
+
FactoryGirl.send(FactoryGirl.factory_by_name(name).default_strategy, name, overrides)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Defines a new sequence that can be used to generate unique values in a specific format.
|
57
|
+
#
|
58
|
+
# Arguments:
|
59
|
+
# name: (Symbol)
|
60
|
+
# A unique name for this sequence. This name will be referenced when
|
61
|
+
# calling next to generate new values from this sequence.
|
62
|
+
# block: (Proc)
|
63
|
+
# The code to generate each value in the sequence. This block will be
|
64
|
+
# called with a unique number each time a value in the sequence is to be
|
65
|
+
# generated. The block should return the generated value for the
|
66
|
+
# sequence.
|
67
|
+
#
|
68
|
+
# Example:
|
69
|
+
#
|
70
|
+
# Factory.sequence(:email) {|n| "somebody_#{n}@example.com" }
|
71
|
+
def self.sequence(name, start_value = 1, &block)
|
72
|
+
FactoryGirl.register_sequence(Sequence.new(name, start_value, &block))
|
73
|
+
end
|
74
|
+
|
75
|
+
# Generates and returns the next value in a sequence.
|
76
|
+
#
|
77
|
+
# Arguments:
|
78
|
+
# name: (Symbol)
|
79
|
+
# The name of the sequence that a value should be generated for.
|
80
|
+
#
|
81
|
+
# Returns:
|
82
|
+
# The next value in the sequence. (Object)
|
83
|
+
def self.next(name)
|
84
|
+
FactoryGirl.generate(name)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Defines a new alias for attributes.
|
88
|
+
#
|
89
|
+
# Arguments:
|
90
|
+
# * pattern: +Regexp+
|
91
|
+
# A pattern that will be matched against attributes when looking for
|
92
|
+
# aliases. Contents captured in the pattern can be used in the alias.
|
93
|
+
# * replace: +String+
|
94
|
+
# The alias that results from the matched pattern. Captured strings can
|
95
|
+
# be substituted like with +String#sub+.
|
96
|
+
#
|
97
|
+
# Example:
|
98
|
+
#
|
99
|
+
# Factory.alias /(.*)_confirmation/, '\1'
|
100
|
+
#
|
101
|
+
# factory_girl starts with aliases for foreign keys, so that a :user
|
102
|
+
# association can be overridden by a :user_id parameter:
|
103
|
+
#
|
104
|
+
# Factory.define :post do |p|
|
105
|
+
# p.association :user
|
106
|
+
# end
|
107
|
+
#
|
108
|
+
# # The user association will not be built in this example. The user_id
|
109
|
+
# # will be used instead.
|
110
|
+
# Factory(:post, :user_id => 1)
|
111
|
+
def self.alias(pattern, replace)
|
112
|
+
FactoryGirl.aliases << [pattern, replace]
|
113
|
+
end
|
114
|
+
|
115
|
+
# Alias for FactoryGirl.attributes_for
|
116
|
+
def self.attributes_for(name, overrides = {})
|
117
|
+
FactoryGirl.attributes_for(name, overrides)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Alias for FactoryGirl.build
|
121
|
+
def self.build(name, overrides = {})
|
122
|
+
FactoryGirl.build(name, overrides)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Alias for FactoryGirl.create
|
126
|
+
def self.create(name, overrides = {})
|
127
|
+
FactoryGirl.create(name, overrides)
|
128
|
+
end
|
129
|
+
|
130
|
+
# Alias for FactoryGirl.build_stubbed.
|
131
|
+
def self.stub(name, overrides = {})
|
132
|
+
FactoryGirl.build_stubbed(name, overrides)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Shortcut for Factory.default_strategy.
|
137
|
+
#
|
138
|
+
# DEPRECATION WARNING:
|
139
|
+
#
|
140
|
+
# In a future release, default_strategy will be removed and this will
|
141
|
+
# simply call create instead.
|
142
|
+
#
|
143
|
+
# Example:
|
144
|
+
# Factory(:user, :name => 'Joe')
|
145
|
+
def Factory(name, attrs = {})
|
146
|
+
Factory.default_strategy(name, attrs)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
include FactoryGirl::Syntax::Vintage
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
# Provides alternate syntaxes for factory_girl. If you don't like the default
|
3
|
+
# syntax for defining or using factories, look at one of the
|
4
|
+
# FactoryGirl::Syntax modules:
|
5
|
+
#
|
6
|
+
# * FactoryGirl::Syntax::Blueprint: definition syntax similar to Machinist
|
7
|
+
# * FactoryGirl::Syntax::Generate: usage syntax similar to Object Daddy
|
8
|
+
# * FactoryGirl::Syntax::Make: usage syntax similar to Machinist
|
9
|
+
# * FactoryGirl::Syntax::Sham: sequence syntax similar to Machinist
|
10
|
+
module Syntax
|
11
|
+
end
|
12
|
+
end
|
data/lib/factory_girl.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'factory_girl/proxy'
|
2
|
+
require 'factory_girl/proxy/build'
|
3
|
+
require 'factory_girl/proxy/create'
|
4
|
+
require 'factory_girl/proxy/attributes_for'
|
5
|
+
require 'factory_girl/proxy/stub'
|
6
|
+
require 'factory_girl/registry'
|
7
|
+
require 'factory_girl/factory'
|
8
|
+
require 'factory_girl/attribute'
|
9
|
+
require 'factory_girl/attribute/static'
|
10
|
+
require 'factory_girl/attribute/dynamic'
|
11
|
+
require 'factory_girl/attribute/association'
|
12
|
+
require 'factory_girl/attribute/callback'
|
13
|
+
require 'factory_girl/attribute/sequence'
|
14
|
+
require 'factory_girl/attribute/implicit'
|
15
|
+
require 'factory_girl/attribute/list'
|
16
|
+
require 'factory_girl/sequence'
|
17
|
+
require 'factory_girl/aliases'
|
18
|
+
require 'factory_girl/definition_proxy'
|
19
|
+
require 'factory_girl/syntax/methods'
|
20
|
+
require 'factory_girl/syntax/default'
|
21
|
+
require 'factory_girl/syntax/vintage'
|
22
|
+
require 'factory_girl/find_definitions'
|
23
|
+
require 'factory_girl/deprecated'
|
24
|
+
require 'factory_girl/version'
|
25
|
+
|
26
|
+
if defined?(Rails) && Rails::VERSION::MAJOR == 2
|
27
|
+
require 'factory_girl/rails2'
|
28
|
+
end
|
29
|
+
|
30
|
+
module FactoryGirl
|
31
|
+
def self.factories
|
32
|
+
@factories ||= Registry.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.register_factory(factory)
|
36
|
+
factories.add(factory)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.factory_by_name(name)
|
40
|
+
factories.find(name)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.sequences
|
44
|
+
@sequences ||= Registry.new
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.register_sequence(sequence)
|
48
|
+
sequences.add(sequence)
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.sequence_by_name(name)
|
52
|
+
sequences.find(name)
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "attribute aliases" do
|
5
|
+
before do
|
6
|
+
define_model('User')
|
7
|
+
|
8
|
+
define_model('Post', :user_id => :integer) do
|
9
|
+
belongs_to :user
|
10
|
+
end
|
11
|
+
|
12
|
+
FactoryGirl.define do
|
13
|
+
factory :user do
|
14
|
+
end
|
15
|
+
|
16
|
+
factory :post do
|
17
|
+
user
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "doesn't assign both an association and its foreign key" do
|
23
|
+
FactoryGirl.build(:post, :user_id => 1).user_id.should == 1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "a generated attributes hash" do
|
5
|
+
include FactoryGirl::Syntax::Methods
|
6
|
+
|
7
|
+
before do
|
8
|
+
define_model('User')
|
9
|
+
|
10
|
+
define_model('Post', :title => :string,
|
11
|
+
:body => :string,
|
12
|
+
:summary => :string,
|
13
|
+
:user_id => :integer) do
|
14
|
+
belongs_to :user
|
15
|
+
end
|
16
|
+
|
17
|
+
FactoryGirl.define do
|
18
|
+
factory :user do
|
19
|
+
end
|
20
|
+
|
21
|
+
factory :post do
|
22
|
+
title { "default title" }
|
23
|
+
body { "default body" }
|
24
|
+
summary { title }
|
25
|
+
user
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
subject { attributes_for(:post, :title => 'overridden title') }
|
31
|
+
|
32
|
+
it "assigns an overridden value" do
|
33
|
+
subject[:title].should == "overridden title"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "assigns a default value" do
|
37
|
+
subject[:body].should == "default body"
|
38
|
+
end
|
39
|
+
|
40
|
+
it "assigns a lazy, dependent attribute" do
|
41
|
+
subject[:summary].should == "overridden title"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "doesn't assign associations" do
|
45
|
+
subject[:user_id].should be_nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "a built instance" do
|
5
|
+
include FactoryGirl::Syntax::Methods
|
6
|
+
|
7
|
+
before do
|
8
|
+
define_model('User')
|
9
|
+
|
10
|
+
define_model('Post', :user_id => :integer) do
|
11
|
+
belongs_to :user
|
12
|
+
end
|
13
|
+
|
14
|
+
FactoryGirl.define do
|
15
|
+
factory :user do
|
16
|
+
end
|
17
|
+
|
18
|
+
factory :post do
|
19
|
+
user
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
subject { build(:post) }
|
25
|
+
|
26
|
+
it "isn't saved" do
|
27
|
+
should be_new_record
|
28
|
+
end
|
29
|
+
|
30
|
+
it "assigns and saves associations" do
|
31
|
+
subject.user.should be_kind_of(User)
|
32
|
+
subject.user.should_not be_new_record
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "a generated stub instance" do
|
5
|
+
include FactoryGirl::Syntax::Methods
|
6
|
+
|
7
|
+
before do
|
8
|
+
define_model('User')
|
9
|
+
|
10
|
+
define_model('Post', :title => :string,
|
11
|
+
:body => :string,
|
12
|
+
:user_id => :integer) do
|
13
|
+
belongs_to :user
|
14
|
+
end
|
15
|
+
|
16
|
+
FactoryGirl.define do
|
17
|
+
factory :user do
|
18
|
+
end
|
19
|
+
|
20
|
+
factory :post do
|
21
|
+
title { "default title" }
|
22
|
+
body { "default body" }
|
23
|
+
user
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
subject { build_stubbed(:post, :title => 'overridden title') }
|
29
|
+
|
30
|
+
it "assigns a default attribute" do
|
31
|
+
subject.body.should == 'default body'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "assigns an overridden attribute" do
|
35
|
+
subject.title.should == 'overridden title'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "assigns associations" do
|
39
|
+
subject.user.should_not be_nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it "has an id" do
|
43
|
+
subject.id.should > 0
|
44
|
+
end
|
45
|
+
|
46
|
+
it "generates unique ids" do
|
47
|
+
other_stub = build_stubbed(:post)
|
48
|
+
subject.id.should_not == other_stub.id
|
49
|
+
end
|
50
|
+
|
51
|
+
it "isn't a new record" do
|
52
|
+
should_not be_new_record
|
53
|
+
end
|
54
|
+
|
55
|
+
it "disables connection" do
|
56
|
+
lambda { subject.connection }.should raise_error(RuntimeError)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "disables update_attribute" do
|
60
|
+
lambda { subject.update_attribute(:title, "value") }.should raise_error(RuntimeError)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "disables reload" do
|
64
|
+
lambda { subject.reload }.should raise_error(RuntimeError)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "disables destroy" do
|
68
|
+
lambda { subject.destroy }.should raise_error(RuntimeError)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "disables save" do
|
72
|
+
lambda { subject.save }.should raise_error(RuntimeError)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "disables increment" do
|
76
|
+
lambda { subject.increment!(:age) }.should raise_error(RuntimeError)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "callbacks" do
|
5
|
+
before do
|
6
|
+
define_model("User", :first_name => :string, :last_name => :string)
|
7
|
+
|
8
|
+
FactoryGirl.define do
|
9
|
+
factory :user_with_callbacks, :class => :user do
|
10
|
+
after_stub { |user| user.first_name = 'Stubby' }
|
11
|
+
after_build { |user| user.first_name = 'Buildy' }
|
12
|
+
after_create { |user| user.last_name = 'Createy' }
|
13
|
+
end
|
14
|
+
|
15
|
+
factory :user_with_inherited_callbacks, :parent => :user_with_callbacks do
|
16
|
+
after_stub { |user| user.last_name = 'Double-Stubby' }
|
17
|
+
end
|
18
|
+
|
19
|
+
factory :user_with_proxied_callbacks, :parent => :user_with_callbacks do |u|
|
20
|
+
u.proxified?(true).ignore
|
21
|
+
after_create { |user, proxy| user.last_name << ', Proxified' if proxy.proxified? }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "runs the after_stub callback when stubbing" do
|
27
|
+
user = FactoryGirl.build_stubbed(:user_with_callbacks)
|
28
|
+
user.first_name.should == 'Stubby'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "runs the after_build callback when building" do
|
32
|
+
user = FactoryGirl.build(:user_with_callbacks)
|
33
|
+
user.first_name.should == 'Buildy'
|
34
|
+
end
|
35
|
+
|
36
|
+
it "runs both the after_build and after_create callbacks when creating" do
|
37
|
+
user = FactoryGirl.create(:user_with_callbacks)
|
38
|
+
user.first_name.should == 'Buildy'
|
39
|
+
user.last_name.should == 'Createy'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "runs both the after_stub callback on the factory and the inherited after_stub callback" do
|
43
|
+
user = FactoryGirl.build_stubbed(:user_with_inherited_callbacks)
|
44
|
+
user.first_name.should == 'Stubby'
|
45
|
+
user.last_name.should == 'Double-Stubby'
|
46
|
+
end
|
47
|
+
|
48
|
+
it "provides the proxy to callbacks that want it" do
|
49
|
+
user = FactoryGirl.create(:user_with_proxied_callbacks)
|
50
|
+
user.first_name.should == 'Buildy'
|
51
|
+
user.last_name.should == 'Createy, Proxified'
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "a created instance" do
|
5
|
+
include FactoryGirl::Syntax::Methods
|
6
|
+
|
7
|
+
before do
|
8
|
+
define_model('User')
|
9
|
+
|
10
|
+
define_model('Post', :user_id => :integer) do
|
11
|
+
belongs_to :user
|
12
|
+
end
|
13
|
+
|
14
|
+
FactoryGirl.define do
|
15
|
+
factory :user do
|
16
|
+
end
|
17
|
+
|
18
|
+
factory :post do
|
19
|
+
user
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
subject { create('post') }
|
25
|
+
|
26
|
+
it "saves" do
|
27
|
+
should_not be_new_record
|
28
|
+
end
|
29
|
+
|
30
|
+
it "assigns and saves associations" do
|
31
|
+
subject.user.should be_kind_of(User)
|
32
|
+
subject.user.should_not be_new_record
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "a custom create" do
|
37
|
+
include FactoryGirl::Syntax::Methods
|
38
|
+
|
39
|
+
before do
|
40
|
+
define_class('User') do
|
41
|
+
def initialize
|
42
|
+
@persisted = false
|
43
|
+
end
|
44
|
+
|
45
|
+
def persist
|
46
|
+
@persisted = true
|
47
|
+
end
|
48
|
+
|
49
|
+
def persisted?
|
50
|
+
@persisted
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
FactoryGirl.define do
|
55
|
+
factory :user do
|
56
|
+
to_create do |user|
|
57
|
+
user.persist
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "uses the custom create block instead of save" do
|
64
|
+
FactoryGirl.create(:user).should be_persisted
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "default strategy" do
|
5
|
+
it "uses create when not specified" do
|
6
|
+
define_model('User')
|
7
|
+
|
8
|
+
FactoryGirl.define do
|
9
|
+
factory :user do
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
Factory(:user).should_not be_new_record
|
14
|
+
end
|
15
|
+
|
16
|
+
it "can be overridden" do
|
17
|
+
define_model('User')
|
18
|
+
|
19
|
+
FactoryGirl.define do
|
20
|
+
factory :user, :default_strategy => :build do
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
Factory(:user).should be_new_record
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "an instance generated by a factory with a custom class name" do
|
5
|
+
before do
|
6
|
+
define_model("User", :admin => :boolean)
|
7
|
+
|
8
|
+
FactoryGirl.define do
|
9
|
+
factory :user do
|
10
|
+
end
|
11
|
+
|
12
|
+
factory :admin, :class => User do
|
13
|
+
admin { true }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
subject { FactoryGirl.create(:admin) }
|
19
|
+
|
20
|
+
it "uses the correct class name" do
|
21
|
+
should be_kind_of(User)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "uses the correct factory definition" do
|
25
|
+
should be_admin
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "an instance generated by a factory that inherits from another factory" do
|
5
|
+
before do
|
6
|
+
define_model("User", :name => :string, :admin => :boolean, :email => :string)
|
7
|
+
|
8
|
+
FactoryGirl.define do
|
9
|
+
factory :user do
|
10
|
+
name { "John" }
|
11
|
+
email { "john@example.com" }
|
12
|
+
end
|
13
|
+
|
14
|
+
factory :admin, :parent => :user do
|
15
|
+
admin { true }
|
16
|
+
email { "admin@example.com" }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
subject { FactoryGirl.create(:admin) }
|
22
|
+
|
23
|
+
it "uses the parent build class" do
|
24
|
+
subject.should be_kind_of(User)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "inherits attributes" do
|
28
|
+
subject.name.should == 'John'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "has its own attributes" do
|
32
|
+
subject.should be_admin
|
33
|
+
end
|
34
|
+
|
35
|
+
it "overrides attributes" do
|
36
|
+
subject.email.should == 'admin@example.com'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'acceptance/acceptance_helper'
|
3
|
+
|
4
|
+
describe "sequences" do
|
5
|
+
include FactoryGirl::Syntax::Methods
|
6
|
+
|
7
|
+
it "generates several values in the correct format" do
|
8
|
+
FactoryGirl.define do
|
9
|
+
sequence :email do |n|
|
10
|
+
"somebody#{n}@example.com"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
first_value = generate(:email)
|
15
|
+
another_value = generate(:email)
|
16
|
+
|
17
|
+
first_value.should =~ /^somebody\d+@example\.com$/
|
18
|
+
another_value.should =~ /^somebody\d+@example\.com$/
|
19
|
+
first_value.should_not == another_value
|
20
|
+
end
|
21
|
+
|
22
|
+
it "generates sequential numbers if no block is given" do
|
23
|
+
FactoryGirl.define do
|
24
|
+
sequence :order
|
25
|
+
end
|
26
|
+
|
27
|
+
first_value = generate(:order)
|
28
|
+
another_value = generate(:order)
|
29
|
+
|
30
|
+
first_value.should == 1
|
31
|
+
another_value.should == 2
|
32
|
+
first_value.should_not == another_value
|
33
|
+
end
|
34
|
+
end
|