factory_girl 2.3.1 → 2.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog +9 -0
- data/Gemfile.lock +1 -1
- data/gemfiles/2.1.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/lib/factory_girl/attribute.rb +10 -24
- data/lib/factory_girl/attribute/association.rb +4 -2
- data/lib/factory_girl/attribute/dynamic.rb +7 -6
- data/lib/factory_girl/attribute/sequence.rb +3 -3
- data/lib/factory_girl/attribute/static.rb +3 -16
- data/lib/factory_girl/attribute_list.rb +3 -27
- data/lib/factory_girl/definition.rb +1 -1
- data/lib/factory_girl/factory.rb +1 -1
- data/lib/factory_girl/proxy.rb +80 -21
- data/lib/factory_girl/proxy/attributes_for.rb +2 -12
- data/lib/factory_girl/proxy/build.rb +2 -20
- data/lib/factory_girl/proxy/create.rb +2 -3
- data/lib/factory_girl/proxy/stub.rb +3 -16
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/attributes_for_spec.rb +17 -0
- data/spec/acceptance/traits_spec.rb +23 -0
- data/spec/factory_girl/attribute/association_spec.rb +14 -13
- data/spec/factory_girl/attribute/dynamic_spec.rb +6 -9
- data/spec/factory_girl/attribute/sequence_spec.rb +1 -3
- data/spec/factory_girl/attribute/static_spec.rb +2 -4
- data/spec/factory_girl/attribute_list_spec.rb +2 -14
- data/spec/factory_girl/attribute_spec.rb +0 -15
- data/spec/factory_girl/declaration/implicit_spec.rb +10 -29
- data/spec/factory_girl/definition_spec.rb +7 -1
- data/spec/factory_girl/factory_spec.rb +1 -1
- data/spec/factory_girl/proxy/attributes_for_spec.rb +10 -5
- data/spec/factory_girl/proxy/create_spec.rb +1 -7
- data/spec/factory_girl/proxy_spec.rb +2 -5
- data/spec/support/shared_examples/proxy.rb +13 -12
- metadata +174 -115
@@ -5,9 +5,8 @@ module FactoryGirl
|
|
5
5
|
|
6
6
|
def initialize(klass, callbacks = [])
|
7
7
|
super
|
8
|
-
|
9
|
-
|
10
|
-
@instance.instance_eval do
|
8
|
+
result_instance.id = next_id
|
9
|
+
result_instance.instance_eval do
|
11
10
|
def persisted?
|
12
11
|
!new_record?
|
13
12
|
end
|
@@ -42,18 +41,6 @@ module FactoryGirl
|
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
|
-
def get(attribute)
|
46
|
-
if @ignored_attributes.has_key?(attribute)
|
47
|
-
@ignored_attributes[attribute]
|
48
|
-
else
|
49
|
-
@instance.send(attribute)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def set(attribute, value)
|
54
|
-
@instance.send(:"#{attribute.name}=", value)
|
55
|
-
end
|
56
|
-
|
57
44
|
def association(factory_name, overrides = {})
|
58
45
|
factory = FactoryGirl.factory_by_name(factory_name)
|
59
46
|
factory.run(Proxy::Stub, overrides.except(:method))
|
@@ -61,7 +48,7 @@ module FactoryGirl
|
|
61
48
|
|
62
49
|
def result(to_create)
|
63
50
|
run_callbacks(:after_stub)
|
64
|
-
|
51
|
+
result_instance
|
65
52
|
end
|
66
53
|
|
67
54
|
private
|
data/lib/factory_girl/version.rb
CHANGED
@@ -61,3 +61,20 @@ describe "calling `attributes_for` with a block" do
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
64
|
+
|
65
|
+
describe "`attributes_for` for a class whose constructor has required params" do
|
66
|
+
before do
|
67
|
+
define_model("User", :name => :string) do
|
68
|
+
def initialize(arg1, arg2); end
|
69
|
+
end
|
70
|
+
|
71
|
+
FactoryGirl.define do
|
72
|
+
factory :user do
|
73
|
+
name "John Doe"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
subject { FactoryGirl.attributes_for(:user) }
|
79
|
+
its([:name]) { should == "John Doe" }
|
80
|
+
end
|
@@ -263,3 +263,26 @@ describe "traits added via proxy" do
|
|
263
263
|
its(:name) { should == "Jack" }
|
264
264
|
end
|
265
265
|
end
|
266
|
+
|
267
|
+
describe "traits and dynamic attributes that are applied simultaneously" do
|
268
|
+
before do
|
269
|
+
define_model("User", :name => :string, :email => :string, :combined => :string)
|
270
|
+
|
271
|
+
FactoryGirl.define do
|
272
|
+
trait :email do
|
273
|
+
email { "#{name}@example.com" }
|
274
|
+
end
|
275
|
+
|
276
|
+
factory :user do
|
277
|
+
name "John"
|
278
|
+
email
|
279
|
+
combined { "#{name} <#{email}>" }
|
280
|
+
end
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
subject { FactoryGirl.build(:user) }
|
285
|
+
its(:name) { should == "John" }
|
286
|
+
its(:email) { should == "John@example.com" }
|
287
|
+
its(:combined) { should == "John <John@example.com>" }
|
288
|
+
end
|
@@ -1,22 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Attribute::Association do
|
4
|
-
let(:name)
|
5
|
-
let(:factory)
|
6
|
-
let(:overrides)
|
7
|
-
let(:
|
4
|
+
let(:name) { :author }
|
5
|
+
let(:factory) { :user }
|
6
|
+
let(:overrides) { { :first_name => "John" } }
|
7
|
+
let(:association) { stub("association") }
|
8
|
+
let(:proxy) { stub("proxy", :association => association) }
|
8
9
|
|
9
|
-
subject
|
10
|
+
subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) }
|
10
11
|
|
11
|
-
it
|
12
|
-
its(:name)
|
13
|
-
its(:factory) { should == factory }
|
12
|
+
it { should be_association }
|
13
|
+
its(:name) { should == name }
|
14
14
|
|
15
|
-
it "
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
it "builds the association when calling the proc" do
|
16
|
+
subject.to_proc(proxy).call.should == association
|
17
|
+
end
|
18
|
+
|
19
|
+
it "builds the association when calling the proc" do
|
20
|
+
subject.to_proc(proxy).call
|
20
21
|
proxy.should have_received(:association).with(factory, overrides)
|
21
22
|
end
|
22
23
|
end
|
@@ -12,18 +12,16 @@ describe FactoryGirl::Attribute::Dynamic do
|
|
12
12
|
context "with a block returning a static value" do
|
13
13
|
let(:block) { lambda { "value" } }
|
14
14
|
|
15
|
-
it "
|
16
|
-
subject.
|
17
|
-
proxy.should have_received(:set).with(subject, "value")
|
15
|
+
it "returns the value when executing the proc" do
|
16
|
+
subject.to_proc(proxy).call.should == "value"
|
18
17
|
end
|
19
18
|
end
|
20
19
|
|
21
20
|
context "with a block returning its block-level variable" do
|
22
21
|
let(:block) { lambda {|thing| thing } }
|
23
22
|
|
24
|
-
it "
|
25
|
-
subject.
|
26
|
-
proxy.should have_received(:set).with(subject, proxy)
|
23
|
+
it "returns the proxy when executing the proc" do
|
24
|
+
subject.to_proc(proxy).call.should == proxy
|
27
25
|
end
|
28
26
|
end
|
29
27
|
|
@@ -36,8 +34,7 @@ describe FactoryGirl::Attribute::Dynamic do
|
|
36
34
|
end
|
37
35
|
|
38
36
|
it "evaluates the attribute from the proxy" do
|
39
|
-
subject.
|
40
|
-
proxy.should have_received(:set).with(subject, result)
|
37
|
+
subject.to_proc(proxy).call.should == result
|
41
38
|
end
|
42
39
|
end
|
43
40
|
|
@@ -45,7 +42,7 @@ describe FactoryGirl::Attribute::Dynamic do
|
|
45
42
|
let(:block) { lambda { Factory.sequence(:email) } }
|
46
43
|
|
47
44
|
it "raises a sequence abuse error" do
|
48
|
-
expect { subject.
|
45
|
+
expect { subject.to_proc(proxy).call }.to raise_error(FactoryGirl::SequenceAbuseError)
|
49
46
|
end
|
50
47
|
end
|
51
48
|
end
|
@@ -12,8 +12,6 @@ describe FactoryGirl::Attribute::Sequence do
|
|
12
12
|
its(:name) { should == name }
|
13
13
|
|
14
14
|
it "assigns the next value in the sequence" do
|
15
|
-
|
16
|
-
subject.add_to(proxy)
|
17
|
-
proxy.should have_received(:set).with(subject, "Name 5")
|
15
|
+
subject.to_proc(proxy).call.should == "Name 5"
|
18
16
|
end
|
19
17
|
end
|
@@ -9,10 +9,8 @@ describe FactoryGirl::Attribute::Static do
|
|
9
9
|
|
10
10
|
its(:name) { should == name }
|
11
11
|
|
12
|
-
it "
|
13
|
-
|
14
|
-
subject.add_to(proxy)
|
15
|
-
proxy.should have_received(:set).with(subject, value)
|
12
|
+
it "returns the value when executing the proc" do
|
13
|
+
subject.to_proc(proxy).call.should == value
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
@@ -51,23 +51,11 @@ describe FactoryGirl::AttributeList, "#apply_attributes" do
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
it "
|
55
|
-
subject.define_attribute(full_name_attribute)
|
56
|
-
subject.apply_attributes(list(city_attribute))
|
57
|
-
subject.to_a.should == [city_attribute, full_name_attribute]
|
58
|
-
end
|
59
|
-
|
60
|
-
it "moves non-static attributes to the end of the list" do
|
61
|
-
subject.define_attribute(full_name_attribute)
|
62
|
-
subject.apply_attributes(list(city_attribute, email_attribute))
|
63
|
-
subject.to_a.should == [city_attribute, full_name_attribute, email_attribute]
|
64
|
-
end
|
65
|
-
|
66
|
-
it "maintains order of non-static attributes" do
|
54
|
+
it "adds attributes in the order defined regardless of attribute type" do
|
67
55
|
subject.define_attribute(full_name_attribute)
|
68
56
|
subject.define_attribute(login_attribute)
|
69
57
|
subject.apply_attributes(list(city_attribute, email_attribute))
|
70
|
-
subject.to_a.should == [
|
58
|
+
subject.to_a.should == [full_name_attribute, login_attribute, city_attribute, email_attribute]
|
71
59
|
end
|
72
60
|
|
73
61
|
it "doesn't overwrite attributes that are already defined" do
|
@@ -8,25 +8,10 @@ describe FactoryGirl::Attribute do
|
|
8
8
|
its(:name) { should == name.to_sym }
|
9
9
|
it { should_not be_association }
|
10
10
|
|
11
|
-
it "doesn't set any attributes on a proxy when added" do
|
12
|
-
proxy.stubs(:set)
|
13
|
-
subject.add_to(proxy)
|
14
|
-
proxy.should have_received(:set).never
|
15
|
-
end
|
16
|
-
|
17
11
|
it "raises an error when defining an attribute writer" do
|
18
12
|
error_message = %{factory_girl uses 'f.test value' syntax rather than 'f.test = value'}
|
19
13
|
expect {
|
20
14
|
FactoryGirl::Attribute.new('test=', false)
|
21
15
|
}.to raise_error(FactoryGirl::AttributeDefinitionError, error_message)
|
22
16
|
end
|
23
|
-
|
24
|
-
it "returns nil when compared to a non-attribute" do
|
25
|
-
(subject <=> "foo").should be_nil
|
26
|
-
end
|
27
|
-
|
28
|
-
it "uses priority to perform comparisons" do
|
29
|
-
second_attribute = FactoryGirl::Attribute.new('name', false)
|
30
|
-
(subject <=> second_attribute).should be_zero
|
31
|
-
end
|
32
17
|
end
|
@@ -1,45 +1,26 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Declaration::Implicit do
|
4
|
-
let(:name)
|
5
|
-
let(:proxy)
|
6
|
-
|
7
|
-
|
4
|
+
let(:name) { :author }
|
5
|
+
let(:proxy) { stub("proxy") }
|
6
|
+
let(:declaration) { FactoryGirl::Declaration::Implicit.new(name) }
|
7
|
+
subject { declaration.to_attributes.first }
|
8
8
|
|
9
9
|
context "with a known factory" do
|
10
10
|
before do
|
11
11
|
FactoryGirl.factories.stubs(:registered? => true)
|
12
12
|
end
|
13
13
|
|
14
|
-
it
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
it "generates an association with the correct factory" do
|
19
|
-
attribute.factory.should == name
|
20
|
-
end
|
21
|
-
|
22
|
-
it "associates the factory" do
|
23
|
-
association = stub("association")
|
24
|
-
proxy.stubs(:set => nil, :association => association)
|
25
|
-
attribute.add_to(proxy)
|
26
|
-
proxy.should have_received(:set).with(attribute, association)
|
27
|
-
proxy.should have_received(:association).with(name, {})
|
28
|
-
end
|
14
|
+
it { should be_association }
|
15
|
+
its(:factory) { should == name }
|
29
16
|
end
|
30
17
|
|
31
18
|
context "with a known sequence" do
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
it "doesn't generate an association" do
|
36
|
-
attribute.should_not be_association
|
19
|
+
before do
|
20
|
+
FactoryGirl.sequences.stubs(:registered? => true)
|
37
21
|
end
|
38
22
|
|
39
|
-
it
|
40
|
-
|
41
|
-
attribute.add_to(proxy)
|
42
|
-
proxy.should have_received(:set).with(attribute, "magic")
|
43
|
-
end
|
23
|
+
it { should_not be_association }
|
24
|
+
it { should be_a(FactoryGirl::Attribute::Sequence) }
|
44
25
|
end
|
45
26
|
end
|
@@ -49,7 +49,13 @@ describe FactoryGirl::Definition, "adding callbacks" do
|
|
49
49
|
end
|
50
50
|
|
51
51
|
describe FactoryGirl::Definition, "#to_create" do
|
52
|
-
its(:to_create) { should
|
52
|
+
its(:to_create) { should be_a(Proc) }
|
53
|
+
|
54
|
+
it "calls save! on the object when run" do
|
55
|
+
instance = stub("model instance", :save! => true)
|
56
|
+
subject.to_create[instance]
|
57
|
+
instance.should have_received(:save!).once
|
58
|
+
end
|
53
59
|
|
54
60
|
it "returns the assigned value when given a block" do
|
55
61
|
block = proc { nil }
|
@@ -271,7 +271,7 @@ describe FactoryGirl::Factory, "running a factory" do
|
|
271
271
|
|
272
272
|
it "returns the result from the proxy when running" do
|
273
273
|
subject.run(FactoryGirl::Proxy::Build, {}).should == "result"
|
274
|
-
proxy.should have_received(:result).with(
|
274
|
+
proxy.should have_received(:result).with(subject.definition.to_create)
|
275
275
|
end
|
276
276
|
|
277
277
|
it "sets overrides once on the factory" do
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Proxy::AttributesFor do
|
4
|
-
let(:proxy_class) { stub("
|
4
|
+
let(:proxy_class) { stub("proxy_class") }
|
5
5
|
|
6
6
|
subject { FactoryGirl::Proxy::AttributesFor.new(proxy_class) }
|
7
7
|
|
@@ -11,18 +11,23 @@ describe FactoryGirl::Proxy::AttributesFor do
|
|
11
11
|
subject.result(nil).should be_kind_of(Hash)
|
12
12
|
end
|
13
13
|
|
14
|
+
it "does not instantiate the proxy class" do
|
15
|
+
proxy_class.stubs(:new)
|
16
|
+
subject.result(nil)
|
17
|
+
proxy_class.should have_received(:new).never
|
18
|
+
end
|
19
|
+
|
14
20
|
describe "after setting an attribute" do
|
15
21
|
let(:attribute) { stub("attribute", :name => :attribute) }
|
16
|
-
let(:value) { "value" }
|
17
22
|
|
18
|
-
before { subject.set(attribute, value) }
|
23
|
+
before { subject.set(attribute, lambda { "value" }) }
|
19
24
|
|
20
25
|
it "sets that value in the resulting hash" do
|
21
|
-
subject.result(nil)[:attribute].should == value
|
26
|
+
subject.result(nil)[:attribute].should == "value"
|
22
27
|
end
|
23
28
|
|
24
29
|
it "returns that value when asked for that attribute" do
|
25
|
-
subject.get(:attribute).should == value
|
30
|
+
subject.get(:attribute).should == "value"
|
26
31
|
end
|
27
32
|
end
|
28
33
|
end
|
@@ -11,11 +11,6 @@ describe FactoryGirl::Proxy::Create do
|
|
11
11
|
it_should_behave_like "proxy with callbacks", :after_build
|
12
12
|
it_should_behave_like "proxy with callbacks", :after_create
|
13
13
|
|
14
|
-
it "saves the instance before returning the result" do
|
15
|
-
subject.result(nil)
|
16
|
-
instance.should have_received(:save!)
|
17
|
-
end
|
18
|
-
|
19
14
|
it "runs a custom create block" do
|
20
15
|
block_run = false
|
21
16
|
block = lambda {|instance| block_run = true }
|
@@ -23,7 +18,6 @@ describe FactoryGirl::Proxy::Create do
|
|
23
18
|
instance.should have_received(:save!).never
|
24
19
|
block_run.should be_true
|
25
20
|
end
|
26
|
-
|
27
21
|
end
|
28
22
|
|
29
23
|
describe FactoryGirl::Proxy::Create, "when running callbacks" do
|
@@ -38,7 +32,7 @@ describe FactoryGirl::Proxy::Create, "when running callbacks" do
|
|
38
32
|
subject { FactoryGirl::Proxy::Create.new(proxy_class, [after_create_one, after_create_two, after_build_one]) }
|
39
33
|
|
40
34
|
it "runs callbacks in the correct order" do
|
41
|
-
subject.result(
|
35
|
+
subject.result(lambda {|instance| instance })
|
42
36
|
callback_result.should == [:after_build_one, :after_create_one, :after_create_two]
|
43
37
|
end
|
44
38
|
end
|
@@ -6,11 +6,8 @@ describe FactoryGirl::Proxy do
|
|
6
6
|
it_should_behave_like "proxy without association support"
|
7
7
|
|
8
8
|
it "doesn't raise when assigning a value to an attribute" do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
it "returns nil for an attribute without a value" do
|
13
|
-
subject.get(:name).should be_nil
|
9
|
+
name_attribute = FactoryGirl::Attribute::Static.new(:name, "great", false)
|
10
|
+
expect { subject.set(name_attribute, lambda { "a name" }) }.to_not raise_error
|
14
11
|
end
|
15
12
|
|
16
13
|
it "calls get for a missing method" do
|
@@ -3,13 +3,12 @@ shared_examples_for "proxy without association support" do
|
|
3
3
|
|
4
4
|
it "does not call FactoryGirl.create when building an association" do
|
5
5
|
FactoryGirl.stubs(:create)
|
6
|
-
subject.set(attribute, "awesome")
|
6
|
+
subject.set(attribute, lambda { "awesome" })
|
7
7
|
FactoryGirl.should have_received(:create).never
|
8
8
|
end
|
9
9
|
|
10
10
|
it "returns nil when accessing an association" do
|
11
|
-
subject.
|
12
|
-
subject.get(:user).should be_nil
|
11
|
+
subject.association(:user, {}).should be_nil
|
13
12
|
end
|
14
13
|
|
15
14
|
it "does not attempt to look up the factory when accessing the association" do
|
@@ -70,7 +69,8 @@ shared_examples_for "proxy with standard getters and setters" do |attribute, val
|
|
70
69
|
|
71
70
|
describe "when setting an attribute" do
|
72
71
|
before do
|
73
|
-
subject.set(attribute_instance, value)
|
72
|
+
subject.set(attribute_instance, lambda { value })
|
73
|
+
subject.result(lambda {|instance| instance })
|
74
74
|
end
|
75
75
|
|
76
76
|
its(attribute) { should == value }
|
@@ -79,19 +79,20 @@ shared_examples_for "proxy with standard getters and setters" do |attribute, val
|
|
79
79
|
|
80
80
|
describe "when setting an ignored attribute" do
|
81
81
|
before do
|
82
|
-
subject.set_ignored(attribute_instance, value)
|
82
|
+
subject.set_ignored(attribute_instance, lambda { value })
|
83
|
+
subject.result(lambda {|instance| instance })
|
83
84
|
end
|
84
85
|
|
85
86
|
it { instance.should have_received(:"#{attribute}=").with(value).never }
|
86
87
|
end
|
87
88
|
|
88
89
|
describe "when getting an attribute" do
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
subject.get(attribute)
|
93
|
-
instance.should have_received(:"#{attribute}")
|
90
|
+
before do
|
91
|
+
subject.set(attribute_instance, lambda { value })
|
92
|
+
subject.result(lambda {|instance| instance })
|
94
93
|
end
|
94
|
+
|
95
|
+
it { subject.get(attribute).should == value }
|
95
96
|
end
|
96
97
|
end
|
97
98
|
|
@@ -102,11 +103,11 @@ shared_examples_for "proxy with callbacks" do |callback_name|
|
|
102
103
|
subject { described_class.new(proxy_class, [callback]) }
|
103
104
|
|
104
105
|
it "runs the #{callback_name} callback" do
|
105
|
-
subject.result(
|
106
|
+
subject.result(lambda {|instance| instance })
|
106
107
|
callback_instance.should have_received(:foo).once
|
107
108
|
end
|
108
109
|
|
109
110
|
it "returns the proxy instance" do
|
110
|
-
subject.result(
|
111
|
+
subject.result(lambda {|instance| instance }).should == instance
|
111
112
|
end
|
112
113
|
end
|