factory_girl 2.5.2 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +0 -1
- data/Changelog +6 -0
- data/GETTING_STARTED.md +52 -7
- data/Gemfile.lock +1 -1
- data/gemfiles/2.3.gemfile.lock +1 -1
- data/gemfiles/3.0.gemfile.lock +1 -1
- data/gemfiles/3.1.gemfile.lock +1 -1
- data/gemfiles/3.2.gemfile.lock +1 -1
- data/lib/factory_girl.rb +2 -1
- data/lib/factory_girl/association_runner.rb +50 -0
- data/lib/factory_girl/definition_proxy.rb +1 -1
- data/lib/factory_girl/evaluator.rb +12 -1
- data/lib/factory_girl/factory.rb +5 -5
- data/lib/factory_girl/strategy.rb +32 -0
- data/lib/factory_girl/{proxy → strategy}/attributes_for.rb +5 -2
- data/lib/factory_girl/strategy/build.rb +15 -0
- data/lib/factory_girl/{proxy → strategy}/create.rb +6 -2
- data/lib/factory_girl/{proxy → strategy}/stub.rb +4 -5
- data/lib/factory_girl/syntax/generate.rb +3 -3
- data/lib/factory_girl/syntax/make.rb +2 -2
- data/lib/factory_girl/syntax/methods.rb +6 -6
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/build_spec.rb +2 -2
- data/spec/acceptance/create_list_spec.rb +41 -0
- data/spec/acceptance/create_spec.rb +39 -10
- data/spec/acceptance/stub_spec.rb +40 -13
- data/spec/acceptance/syntax/vintage_spec.rb +10 -10
- data/spec/acceptance/traits_spec.rb +1 -1
- data/spec/acceptance/transient_attributes_spec.rb +2 -2
- data/spec/factory_girl/association_runner_spec.rb +31 -0
- data/spec/factory_girl/attribute/association_spec.rb +0 -1
- data/spec/factory_girl/attribute_spec.rb +0 -1
- data/spec/factory_girl/declaration/implicit_spec.rb +0 -1
- data/spec/factory_girl/factory_spec.rb +16 -16
- data/spec/factory_girl/{proxy → strategy}/attributes_for_spec.rb +2 -2
- data/spec/factory_girl/strategy/build_spec.rb +7 -0
- data/spec/factory_girl/{proxy → strategy}/create_spec.rb +3 -3
- data/spec/factory_girl/{proxy → strategy}/stub_spec.rb +4 -4
- data/spec/factory_girl/strategy_spec.rb +21 -0
- data/spec/support/shared_examples/strategy.rb +112 -0
- metadata +47 -48
- data/gemfiles/2.1.gemfile +0 -8
- data/gemfiles/2.1.gemfile.lock +0 -74
- data/lib/factory_girl/proxy.rb +0 -64
- data/lib/factory_girl/proxy/build.rb +0 -27
- data/spec/factory_girl/proxy/build_spec.rb +0 -7
- data/spec/factory_girl/proxy_spec.rb +0 -19
- data/spec/support/shared_examples/proxy.rb +0 -90
data/lib/factory_girl/version.rb
CHANGED
@@ -31,7 +31,7 @@ describe "a built instance" do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
describe "a built instance with :
|
34
|
+
describe "a built instance with :strategy => :build" do
|
35
35
|
include FactoryGirl::Syntax::Methods
|
36
36
|
|
37
37
|
before do
|
@@ -45,7 +45,7 @@ describe "a built instance with :method => :build" do
|
|
45
45
|
factory :user
|
46
46
|
|
47
47
|
factory :post do
|
48
|
-
association(:user, :
|
48
|
+
association(:user, :strategy => :build)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
@@ -39,3 +39,44 @@ describe "create multiple instances" do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
describe "multiple creates and ignored attributes to dynamically build attribute lists" do
|
44
|
+
before do
|
45
|
+
define_model('User', :name => :string) do
|
46
|
+
has_many :posts
|
47
|
+
end
|
48
|
+
|
49
|
+
define_model('Post', :title => :string, :user_id => :integer) do
|
50
|
+
belongs_to :user
|
51
|
+
end
|
52
|
+
|
53
|
+
FactoryGirl.define do
|
54
|
+
factory :post do
|
55
|
+
title "Through the Looking Glass"
|
56
|
+
user
|
57
|
+
end
|
58
|
+
|
59
|
+
factory :user do
|
60
|
+
name "John Doe"
|
61
|
+
|
62
|
+
factory :user_with_posts do
|
63
|
+
ignore do
|
64
|
+
posts_count 5
|
65
|
+
end
|
66
|
+
|
67
|
+
after_create do |user, evaluator|
|
68
|
+
FactoryGirl.create_list(:post, evaluator.posts_count, :user => user)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
it "generates the correct number of posts" do
|
76
|
+
FactoryGirl.create(:user_with_posts).posts.length.should == 5
|
77
|
+
end
|
78
|
+
|
79
|
+
it "allows the number of posts to be modified" do
|
80
|
+
FactoryGirl.create(:user_with_posts, :posts_count => 2).posts.length.should == 2
|
81
|
+
end
|
82
|
+
end
|
@@ -31,30 +31,59 @@ describe "a created instance" do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
describe "a created instance, specifying :
|
34
|
+
describe "a created instance, specifying :strategy => build" do
|
35
35
|
include FactoryGirl::Syntax::Methods
|
36
36
|
|
37
|
-
|
38
|
-
|
37
|
+
def define_factories_with_method
|
38
|
+
FactoryGirl.define do
|
39
|
+
factory :user
|
39
40
|
|
40
|
-
|
41
|
-
|
41
|
+
factory :post do
|
42
|
+
association(:user, :method => :build)
|
43
|
+
end
|
42
44
|
end
|
45
|
+
end
|
43
46
|
|
47
|
+
def define_factories_with_strategy
|
44
48
|
FactoryGirl.define do
|
45
49
|
factory :user
|
46
50
|
|
47
51
|
factory :post do
|
48
|
-
association(:user, :
|
52
|
+
association(:user, :strategy => :build)
|
49
53
|
end
|
50
54
|
end
|
51
55
|
end
|
52
56
|
|
53
|
-
|
57
|
+
before do
|
58
|
+
define_model('User')
|
54
59
|
|
55
|
-
|
56
|
-
|
57
|
-
|
60
|
+
define_model('Post', :user_id => :integer) do
|
61
|
+
belongs_to :user
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "associations declared with :strategy" do
|
66
|
+
before { define_factories_with_strategy }
|
67
|
+
subject { build_stubbed(:post) }
|
68
|
+
|
69
|
+
subject { create('post') }
|
70
|
+
|
71
|
+
it "still saves associations (:strategy => :build only affects build, not create)" do
|
72
|
+
subject.user.should be_kind_of(User)
|
73
|
+
subject.user.should_not be_new_record
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "associations declared with :method" do
|
78
|
+
before { define_factories_with_method }
|
79
|
+
subject { build_stubbed(:post) }
|
80
|
+
|
81
|
+
subject { create('post') }
|
82
|
+
|
83
|
+
it "still saves associations (:method => :build only affects build, not create)" do
|
84
|
+
subject.user.should be_kind_of(User)
|
85
|
+
subject.user.should_not be_new_record
|
86
|
+
end
|
58
87
|
end
|
59
88
|
end
|
60
89
|
|
@@ -31,34 +31,61 @@ describe "a stubbed instance" do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
describe "a stubbed instance
|
34
|
+
describe "a stubbed instance overriding strategy" do
|
35
35
|
include FactoryGirl::Syntax::Methods
|
36
36
|
|
37
|
-
|
38
|
-
|
37
|
+
def define_factories_with_method
|
38
|
+
FactoryGirl.define do
|
39
|
+
factory :user
|
39
40
|
|
40
|
-
|
41
|
-
|
41
|
+
factory :post do
|
42
|
+
association(:user, :method => :build)
|
43
|
+
end
|
42
44
|
end
|
45
|
+
end
|
43
46
|
|
47
|
+
def define_factories_with_strategy
|
44
48
|
FactoryGirl.define do
|
45
49
|
factory :user
|
46
50
|
|
47
51
|
factory :post do
|
48
|
-
association(:user, :
|
52
|
+
association(:user, :strategy => :build)
|
49
53
|
end
|
50
54
|
end
|
51
55
|
end
|
52
56
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
+
before do
|
58
|
+
define_model('User')
|
59
|
+
define_model('Post', :user_id => :integer) do
|
60
|
+
belongs_to :user
|
61
|
+
end
|
57
62
|
end
|
58
63
|
|
59
|
-
|
60
|
-
|
61
|
-
subject
|
64
|
+
context "associations declared with :strategy" do
|
65
|
+
before { define_factories_with_strategy }
|
66
|
+
subject { build_stubbed(:post) }
|
67
|
+
|
68
|
+
it "acts as if it is saved in the database" do
|
69
|
+
should_not be_new_record
|
70
|
+
end
|
71
|
+
|
72
|
+
it "assigns associations and acts as if it is saved" do
|
73
|
+
subject.user.should be_kind_of(User)
|
74
|
+
subject.user.should_not be_new_record
|
75
|
+
end
|
62
76
|
end
|
63
77
|
|
78
|
+
context "associations declared with :method" do
|
79
|
+
before { define_factories_with_method }
|
80
|
+
subject { build_stubbed(:post) }
|
81
|
+
|
82
|
+
it "acts as if it is saved in the database" do
|
83
|
+
should_not be_new_record
|
84
|
+
end
|
85
|
+
|
86
|
+
it "assigns associations and acts as if it is saved" do
|
87
|
+
subject.user.should be_kind_of(User)
|
88
|
+
subject.user.should_not be_new_record
|
89
|
+
end
|
90
|
+
end
|
64
91
|
end
|
@@ -93,40 +93,40 @@ describe "after defining a factory" do
|
|
93
93
|
FactoryGirl.register_factory(@factory)
|
94
94
|
end
|
95
95
|
|
96
|
-
it "uses
|
96
|
+
it "uses Strategy::AttributesFor for Factory.attributes_for" do
|
97
97
|
@factory.stubs(:run => "result")
|
98
98
|
Factory.attributes_for(@name, :attr => 'value').should == 'result'
|
99
|
-
@factory.should have_received(:run).with(FactoryGirl::
|
99
|
+
@factory.should have_received(:run).with(FactoryGirl::Strategy::AttributesFor, :attr => 'value')
|
100
100
|
end
|
101
101
|
|
102
|
-
it "uses
|
102
|
+
it "uses Strategy::Build for Factory.build" do
|
103
103
|
@factory.stubs(:run => "result")
|
104
104
|
Factory.build(@name, :attr => 'value').should == 'result'
|
105
|
-
@factory.should have_received(:run).with(FactoryGirl::
|
105
|
+
@factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :attr => 'value')
|
106
106
|
end
|
107
107
|
|
108
|
-
it "uses
|
108
|
+
it "uses Strategy::Create for Factory.create" do
|
109
109
|
@factory.stubs(:run => "result")
|
110
110
|
Factory.create(@name, :attr => 'value').should == 'result'
|
111
|
-
@factory.should have_received(:run).with(FactoryGirl::
|
111
|
+
@factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :attr => 'value')
|
112
112
|
end
|
113
113
|
|
114
|
-
it "uses
|
114
|
+
it "uses Strategy::Stub for Factory.stub" do
|
115
115
|
@factory.stubs(:run => "result")
|
116
116
|
Factory.stub(@name, :attr => 'value').should == 'result'
|
117
|
-
@factory.should have_received(:run).with(FactoryGirl::
|
117
|
+
@factory.should have_received(:run).with(FactoryGirl::Strategy::Stub, :attr => 'value')
|
118
118
|
end
|
119
119
|
|
120
120
|
it "uses default strategy option as Factory.default_strategy" do
|
121
121
|
@factory.stubs(:default_strategy => :create, :run => "result")
|
122
122
|
Factory.default_strategy(@name, :attr => 'value').should == 'result'
|
123
|
-
@factory.should have_received(:run).with(FactoryGirl::
|
123
|
+
@factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :attr => 'value')
|
124
124
|
end
|
125
125
|
|
126
126
|
it "uses the default strategy for the global Factory method" do
|
127
127
|
@factory.stubs(:default_strategy => :create, :run => "result")
|
128
128
|
Factory(@name, :attr => 'value').should == 'result'
|
129
|
-
@factory.should have_received(:run).with(FactoryGirl::
|
129
|
+
@factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :attr => 'value')
|
130
130
|
end
|
131
131
|
|
132
132
|
[:build, :create, :attributes_for, :stub].each do |method|
|
@@ -17,8 +17,8 @@ describe "transient attributes" do
|
|
17
17
|
name { "#{FactoryGirl.generate(:name)}#{" - Rockstar" if rockstar}" }
|
18
18
|
email { "#{name.downcase}#{four}@example.com" }
|
19
19
|
|
20
|
-
after_create do |user,
|
21
|
-
user.name.upcase! if
|
20
|
+
after_create do |user, evaluator|
|
21
|
+
user.name.upcase! if evaluator.upcased
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe FactoryGirl::AssociationRunner do
|
4
|
+
let(:factory) { stub("factory", :run => instance) }
|
5
|
+
let(:instance) { stub("instance") }
|
6
|
+
|
7
|
+
before do
|
8
|
+
FactoryGirl.stubs(:factory_by_name => factory)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "passes all overrides to the factory" do
|
12
|
+
FactoryGirl::AssociationRunner.new(:user, FactoryGirl::Strategy::Build, :strategy => :build, :name => "John").run
|
13
|
+
factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :strategy => :build, :name => "John")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "runs a strategy inferred by name based on a factory name" do
|
17
|
+
FactoryGirl::AssociationRunner.new(:user, :build, :strategy => :build, :name => "John").run
|
18
|
+
factory.should have_received(:run).with(FactoryGirl::Strategy::Build, :strategy => :build, :name => "John")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "allows overriding strategy" do
|
22
|
+
FactoryGirl::AssociationRunner.new(:user, :build, :strategy => :build, :name => "John").run(FactoryGirl::Strategy::Create)
|
23
|
+
factory.should have_received(:run).with(FactoryGirl::Strategy::Create, :strategy => :build, :name => "John")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "raises if the strategy cannot be inferred" do
|
27
|
+
expect do
|
28
|
+
FactoryGirl::AssociationRunner.new(:user, :bogus_strategy, :strategy => :build, :name => "John").run
|
29
|
+
end.to raise_error("unrecognized method bogus_strategy")
|
30
|
+
end
|
31
|
+
end
|
@@ -5,7 +5,6 @@ describe FactoryGirl::Attribute::Association do
|
|
5
5
|
let(:factory) { :user }
|
6
6
|
let(:overrides) { { :first_name => "John" } }
|
7
7
|
let(:association) { stub("association") }
|
8
|
-
let(:proxy) { stub("proxy", :association => association) }
|
9
8
|
|
10
9
|
subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) }
|
11
10
|
before { subject.stubs(:association => association) }
|
@@ -25,15 +25,15 @@ describe FactoryGirl::Factory do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "passes a custom creation block" do
|
28
|
-
|
29
|
-
FactoryGirl::
|
28
|
+
strategy = stub("strategy", :result => nil, :add_observer => true)
|
29
|
+
FactoryGirl::Strategy::Build.stubs(:new => strategy)
|
30
30
|
block = lambda {}
|
31
31
|
factory = FactoryGirl::Factory.new(:object)
|
32
32
|
factory.to_create(&block)
|
33
33
|
|
34
|
-
factory.run(FactoryGirl::
|
34
|
+
factory.run(FactoryGirl::Strategy::Build, {})
|
35
35
|
|
36
|
-
|
36
|
+
strategy.should have_received(:result).with(instance_of(FactoryGirl::AttributeAssigner), block)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "returns associations" do
|
@@ -72,21 +72,21 @@ describe FactoryGirl::Factory do
|
|
72
72
|
it "returns the overridden value in the generated attributes" do
|
73
73
|
declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
|
74
74
|
@factory.declare_attribute(declaration)
|
75
|
-
result = @factory.run(FactoryGirl::
|
75
|
+
result = @factory.run(FactoryGirl::Strategy::AttributesFor, @hash)
|
76
76
|
result[@name].should == @value
|
77
77
|
end
|
78
78
|
|
79
79
|
it "does not call a lazy attribute block for an overridden attribute" do
|
80
80
|
declaration = FactoryGirl::Declaration::Dynamic.new(@name, false, lambda { flunk })
|
81
81
|
@factory.declare_attribute(declaration)
|
82
|
-
@factory.run(FactoryGirl::
|
82
|
+
@factory.run(FactoryGirl::Strategy::AttributesFor, @hash)
|
83
83
|
end
|
84
84
|
|
85
85
|
it "overrides a symbol parameter with a string parameter" do
|
86
86
|
declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
|
87
87
|
@factory.declare_attribute(declaration)
|
88
88
|
@hash = { @name.to_s => @value }
|
89
|
-
result = @factory.run(FactoryGirl::
|
89
|
+
result = @factory.run(FactoryGirl::Strategy::AttributesFor, @hash)
|
90
90
|
result[@name].should == @value
|
91
91
|
end
|
92
92
|
end
|
@@ -95,7 +95,7 @@ describe FactoryGirl::Factory do
|
|
95
95
|
before do
|
96
96
|
@factory.declare_attribute(FactoryGirl::Declaration::Static.new(:test, 'original'))
|
97
97
|
Factory.alias(/(.*)_alias/, '\1')
|
98
|
-
@result = @factory.run(FactoryGirl::
|
98
|
+
@result = @factory.run(FactoryGirl::Strategy::AttributesFor,
|
99
99
|
:test_alias => 'new')
|
100
100
|
end
|
101
101
|
|
@@ -279,7 +279,7 @@ describe FactoryGirl::Factory, "running a factory" do
|
|
279
279
|
subject { FactoryGirl::Factory.new(:user) }
|
280
280
|
let(:attribute) { FactoryGirl::Attribute::Static.new(:name, "value", false) }
|
281
281
|
let(:declaration) { FactoryGirl::Declaration::Static.new(:name, "value", false) }
|
282
|
-
let(:
|
282
|
+
let(:strategy) { stub("strategy", :result => "result", :add_observer => true) }
|
283
283
|
let(:attributes) { [attribute] }
|
284
284
|
let(:attribute_list) { stub('attribute-list', :declarations => [declaration], :to_a => attributes) }
|
285
285
|
|
@@ -287,23 +287,23 @@ describe FactoryGirl::Factory, "running a factory" do
|
|
287
287
|
define_model("User", :name => :string)
|
288
288
|
FactoryGirl::Declaration::Static.stubs(:new => declaration)
|
289
289
|
declaration.stubs(:to_attributes => attributes)
|
290
|
-
FactoryGirl::
|
290
|
+
FactoryGirl::Strategy::Build.stubs(:new => strategy)
|
291
291
|
subject.declare_attribute(declaration)
|
292
292
|
end
|
293
293
|
|
294
|
-
it "creates the right
|
295
|
-
subject.run(FactoryGirl::
|
296
|
-
FactoryGirl::
|
294
|
+
it "creates the right strategy using the build class when running" do
|
295
|
+
subject.run(FactoryGirl::Strategy::Build, {})
|
296
|
+
FactoryGirl::Strategy::Build.should have_received(:new).once
|
297
297
|
end
|
298
298
|
|
299
|
-
it "returns the result from the
|
300
|
-
subject.run(FactoryGirl::
|
299
|
+
it "returns the result from the strategy when running" do
|
300
|
+
subject.run(FactoryGirl::Strategy::Build, {}).should == "result"
|
301
301
|
end
|
302
302
|
|
303
303
|
it "calls the block and returns the result" do
|
304
304
|
block_run = nil
|
305
305
|
block = lambda {|result| block_run = "changed" }
|
306
|
-
subject.run(FactoryGirl::
|
306
|
+
subject.run(FactoryGirl::Strategy::Build, { }, &block)
|
307
307
|
block_run.should == "changed"
|
308
308
|
end
|
309
309
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe FactoryGirl::
|
3
|
+
describe FactoryGirl::Strategy::AttributesFor do
|
4
4
|
let(:result) { { :name => "John Doe", :gender => "Male", :admin => false } }
|
5
5
|
let(:attribute_assigner) { stub("attribute assigner", :hash => result) }
|
6
6
|
|
7
|
-
it_should_behave_like "
|
7
|
+
it_should_behave_like "strategy without association support"
|
8
8
|
|
9
9
|
it "returns the hash from the attribute assigner" do
|
10
10
|
subject.result(attribute_assigner, lambda {|item| item }).should == result
|