factory_girl 2.0.4 → 2.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CONTRIBUTION_GUIDELINES.md +1 -0
- data/GETTING_STARTED.md +28 -4
- data/Gemfile +2 -1
- data/Gemfile.lock +6 -2
- data/features/factory_girl_steps.feature +17 -0
- data/features/step_definitions/database_steps.rb +22 -0
- data/features/support/factories.rb +13 -0
- data/features/support/test.db +0 -0
- data/lib/factory_girl/aliases.rb +1 -3
- data/lib/factory_girl/attribute.rb +16 -8
- data/lib/factory_girl/attribute/association.rb +0 -3
- data/lib/factory_girl/attribute/callback.rb +0 -2
- data/lib/factory_girl/attribute/dynamic.rb +1 -3
- data/lib/factory_girl/attribute/static.rb +1 -1
- data/lib/factory_girl/attribute_list.rb +30 -9
- data/lib/factory_girl/factory.rb +1 -1
- data/lib/factory_girl/proxy.rb +7 -5
- data/lib/factory_girl/proxy/attributes_for.rb +8 -3
- data/lib/factory_girl/proxy/build.rb +12 -3
- data/lib/factory_girl/proxy/stub.rb +12 -3
- data/lib/factory_girl/sequence.rb +2 -2
- data/lib/factory_girl/step_definitions.rb +3 -2
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/attribute_aliases_spec.rb +0 -1
- data/spec/acceptance/attributes_for_spec.rb +0 -1
- data/spec/acceptance/attributes_ordered_spec.rb +24 -6
- data/spec/acceptance/build_list_spec.rb +0 -1
- data/spec/acceptance/build_spec.rb +0 -1
- data/spec/acceptance/build_stubbed_spec.rb +0 -1
- data/spec/acceptance/callbacks_spec.rb +0 -1
- data/spec/acceptance/create_list_spec.rb +0 -1
- data/spec/acceptance/create_spec.rb +0 -1
- data/spec/acceptance/default_strategy_spec.rb +0 -1
- data/spec/acceptance/definition_spec.rb +0 -1
- data/spec/acceptance/definition_without_block_spec.rb +0 -1
- data/spec/acceptance/overrides_spec.rb +0 -1
- data/spec/acceptance/parent_spec.rb +18 -1
- data/spec/acceptance/sequence_spec.rb +0 -1
- data/spec/acceptance/syntax/blueprint_spec.rb +0 -1
- data/spec/acceptance/syntax/generate_spec.rb +0 -1
- data/spec/acceptance/syntax/make_spec.rb +0 -1
- data/spec/acceptance/syntax/sham_spec.rb +0 -1
- data/spec/acceptance/syntax/vintage_spec.rb +27 -29
- data/spec/acceptance/traits_spec.rb +0 -1
- data/spec/acceptance/transient_attributes_spec.rb +68 -0
- data/spec/factory_girl/aliases_spec.rb +19 -21
- data/spec/factory_girl/attribute/association_spec.rb +16 -24
- data/spec/factory_girl/attribute/callback_spec.rb +14 -15
- data/spec/factory_girl/attribute/dynamic_spec.rb +41 -45
- data/spec/factory_girl/attribute/implicit_spec.rb +18 -30
- data/spec/factory_girl/attribute/sequence_spec.rb +11 -13
- data/spec/factory_girl/attribute/static_spec.rb +13 -20
- data/spec/factory_girl/attribute_list_spec.rb +9 -1
- data/spec/factory_girl/attribute_spec.rb +17 -28
- data/spec/factory_girl/definition_proxy_spec.rb +131 -82
- data/spec/factory_girl/deprecated_spec.rb +15 -36
- data/spec/factory_girl/factory_spec.rb +106 -135
- data/spec/factory_girl/find_definitions_spec.rb +7 -6
- data/spec/factory_girl/proxy/attributes_for_spec.rb +23 -32
- data/spec/factory_girl/proxy/build_spec.rb +6 -80
- data/spec/factory_girl/proxy/create_spec.rb +24 -89
- data/spec/factory_girl/proxy/stub_spec.rb +14 -73
- data/spec/factory_girl/proxy_spec.rb +53 -53
- data/spec/factory_girl/registry_spec.rb +13 -29
- data/spec/factory_girl/sequence_spec.rb +24 -72
- data/spec/factory_girl_spec.rb +10 -5
- data/spec/spec_helper.rb +5 -79
- data/spec/support/macros/define_constant.rb +86 -0
- data/spec/support/shared_examples/proxy.rb +99 -0
- metadata +34 -20
- data/spec/acceptance/acceptance_helper.rb +0 -11
@@ -0,0 +1,68 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "transient attributes" do
|
4
|
+
before do
|
5
|
+
define_model("User", :name => :string, :email => :string)
|
6
|
+
|
7
|
+
FactoryGirl.define do
|
8
|
+
sequence(:name) {|n| "John #{n}" }
|
9
|
+
|
10
|
+
factory :user do
|
11
|
+
four { 2 + 2 }.ignore
|
12
|
+
rockstar(true).ignore
|
13
|
+
upcased(false).ignore
|
14
|
+
|
15
|
+
name { "#{FactoryGirl.generate(:name)}#{" - Rockstar" if rockstar}" }
|
16
|
+
email { "#{name.downcase}#{four}@example.com" }
|
17
|
+
|
18
|
+
after_create do |user, proxy|
|
19
|
+
user.name.upcase! if proxy.upcased
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "returning attributes for a factory" do
|
26
|
+
subject { FactoryGirl.attributes_for(:user, :rockstar => true) }
|
27
|
+
it { should_not have_key(:four) }
|
28
|
+
it { should_not have_key(:rockstar) }
|
29
|
+
it { should_not have_key(:upcased) }
|
30
|
+
it { should have_key(:name) }
|
31
|
+
it { should have_key(:email) }
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with a transient variable assigned" do
|
35
|
+
let(:rockstar) { FactoryGirl.create(:user, :rockstar => true, :four => "1234") }
|
36
|
+
let(:rockstar_with_name) { FactoryGirl.create(:user, :name => "Jane Doe", :rockstar => true) }
|
37
|
+
let(:upcased_rockstar) { FactoryGirl.create(:user, :rockstar => true, :upcased => true) }
|
38
|
+
let(:groupie) { FactoryGirl.create(:user, :rockstar => false) }
|
39
|
+
|
40
|
+
it "generates the correct attributes on a rockstar" do
|
41
|
+
rockstar.name.should == "John 1 - Rockstar"
|
42
|
+
rockstar.email.should == "john 1 - rockstar1234@example.com"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "generates the correct attributes on an upcased rockstar" do
|
46
|
+
upcased_rockstar.name.should == "JOHN 1 - ROCKSTAR"
|
47
|
+
upcased_rockstar.email.should == "john 1 - rockstar4@example.com"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "generates the correct attributes on a groupie" do
|
51
|
+
groupie.name.should == "John 1"
|
52
|
+
groupie.email.should == "john 14@example.com"
|
53
|
+
end
|
54
|
+
|
55
|
+
it "generates the correct attributes on a rockstar with a name" do
|
56
|
+
rockstar_with_name.name.should == "Jane Doe"
|
57
|
+
rockstar_with_name.email.should == "jane doe4@example.com"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "without transient variables assigned" do
|
62
|
+
let(:rockstar) { FactoryGirl.create(:user) }
|
63
|
+
|
64
|
+
it "uses the default value of the attribute" do
|
65
|
+
rockstar.name.should == "John 1 - Rockstar"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -1,33 +1,31 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
describe FactoryGirl, "aliases" do
|
4
|
+
context "aliases for an attribute" do
|
5
|
+
subject { FactoryGirl.aliases_for(:test) }
|
6
|
+
it { should include(:test) }
|
7
|
+
it { should include(:test_id) }
|
7
8
|
end
|
8
9
|
|
9
|
-
|
10
|
-
FactoryGirl.aliases_for(:test_id)
|
10
|
+
context "aliases for a foreign key" do
|
11
|
+
subject { FactoryGirl.aliases_for(:test_id) }
|
12
|
+
it { should include(:test) }
|
13
|
+
it { should include(:test_id) }
|
11
14
|
end
|
12
15
|
|
13
|
-
|
14
|
-
FactoryGirl.aliases_for(:
|
16
|
+
context "aliases for an attribute starting with an underscore" do
|
17
|
+
subject { FactoryGirl.aliases_for(:_id) }
|
18
|
+
it { should_not include(:id) }
|
15
19
|
end
|
20
|
+
end
|
16
21
|
|
17
|
-
|
18
|
-
|
22
|
+
describe Factory, "after defining an alias" do
|
23
|
+
before do
|
24
|
+
Factory.alias(/(.*)_suffix/, '\1')
|
19
25
|
end
|
20
26
|
|
21
|
-
|
22
|
-
|
23
|
-
before do
|
24
|
-
Factory.alias(/(.*)_suffix/, '\1')
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should return the alias in the aliases list" do
|
28
|
-
FactoryGirl.aliases_for(:test_suffix).should include(:test)
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
27
|
+
subject { FactoryGirl.aliases_for(:test_suffix) }
|
32
28
|
|
29
|
+
it { should include(:test) }
|
30
|
+
it { should include(:test_suffix_id) }
|
33
31
|
end
|
@@ -1,33 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Attribute::Association do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@attr = FactoryGirl::Attribute::Association.new(@name, @factory, @overrides)
|
9
|
-
end
|
4
|
+
let(:name) { :author }
|
5
|
+
let(:factory) { :user }
|
6
|
+
let(:overrides) { { :first_name => "John" } }
|
7
|
+
let(:proxy) { stub("proxy") }
|
10
8
|
|
11
|
-
|
12
|
-
@attr.name.should == @name
|
13
|
-
end
|
9
|
+
subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) }
|
14
10
|
|
15
|
-
it
|
16
|
-
|
17
|
-
|
11
|
+
it { should be_association }
|
12
|
+
its(:name) { should == name }
|
13
|
+
its(:factory) { should == factory }
|
18
14
|
|
19
|
-
it "
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
it "should tell the proxy to associate when being added to a proxy" do
|
24
|
-
proxy = "proxy"
|
25
|
-
stub(proxy).associate
|
26
|
-
@attr.add_to(proxy)
|
27
|
-
proxy.should have_received.associate(@name, @factory, @overrides)
|
15
|
+
it "tells the proxy to create an association when being added" do
|
16
|
+
proxy.stubs(:associate)
|
17
|
+
subject.add_to(proxy)
|
18
|
+
proxy.should have_received(:associate).with(name, factory, overrides)
|
28
19
|
end
|
20
|
+
end
|
29
21
|
|
30
|
-
|
31
|
-
FactoryGirl::Attribute::Association.new(
|
32
|
-
|
22
|
+
describe FactoryGirl::Attribute::Association, "with a string name" do
|
23
|
+
subject { FactoryGirl::Attribute::Association.new("name", :user, {}) }
|
24
|
+
its(:name) { should == :name }
|
33
25
|
end
|
@@ -1,23 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Attribute::Callback do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
@attr = FactoryGirl::Attribute::Callback.new(@name, @block)
|
8
|
-
end
|
4
|
+
let(:name) { :after_create }
|
5
|
+
let(:block) { proc { "block" } }
|
6
|
+
let(:proxy) { stub("proxy") }
|
9
7
|
|
10
|
-
|
11
|
-
@attr.name.should == @name
|
12
|
-
end
|
8
|
+
subject { FactoryGirl::Attribute::Callback.new(name, block) }
|
13
9
|
|
14
|
-
|
15
|
-
@proxy = "proxy"
|
16
|
-
mock(@proxy).add_callback(@name, @block)
|
17
|
-
@attr.add_to(@proxy)
|
18
|
-
end
|
10
|
+
its(:name) { should == name }
|
19
11
|
|
20
|
-
it "
|
21
|
-
|
12
|
+
it "set its callback on a proxy" do
|
13
|
+
proxy.stubs(:add_callback)
|
14
|
+
subject.add_to(proxy)
|
15
|
+
proxy.should have_received(:add_callback).with(name, block)
|
22
16
|
end
|
23
17
|
end
|
18
|
+
|
19
|
+
describe FactoryGirl::Attribute::Callback, "with a string name" do
|
20
|
+
subject { FactoryGirl::Attribute::Callback.new("name", nil) }
|
21
|
+
its(:name) { should == :name }
|
22
|
+
end
|
@@ -1,60 +1,56 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Attribute::Dynamic do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
@attr = FactoryGirl::Attribute::Dynamic.new(@name, @block)
|
8
|
-
end
|
4
|
+
let(:name) { :first_name }
|
5
|
+
let(:proxy) { stub("proxy", :set => nil) }
|
6
|
+
let(:block) { lambda { } }
|
9
7
|
|
10
|
-
|
11
|
-
@attr.name.should == @name
|
12
|
-
end
|
8
|
+
subject { FactoryGirl::Attribute::Dynamic.new(name, block) }
|
13
9
|
|
14
|
-
|
15
|
-
@proxy = "proxy"
|
16
|
-
stub(@proxy).set
|
17
|
-
@attr.add_to(@proxy)
|
18
|
-
@proxy.should have_received.set(@name, 'value')
|
19
|
-
end
|
10
|
+
its(:name) { should == name }
|
20
11
|
|
21
|
-
|
22
|
-
|
23
|
-
@attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
|
24
|
-
@proxy = "proxy"
|
25
|
-
stub(@proxy).set
|
26
|
-
@attr.add_to(@proxy)
|
27
|
-
@proxy.should have_received.set(:user, @proxy)
|
28
|
-
end
|
12
|
+
context "with a block returning a static value" do
|
13
|
+
let(:block) { lambda { "value" } }
|
29
14
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
@proxy = "proxy"
|
35
|
-
stub(@proxy).set
|
36
|
-
stub(@proxy).other_attribute { result }
|
37
|
-
@attr.add_to(@proxy)
|
38
|
-
@proxy.should have_received.set(:user, result)
|
15
|
+
it "calls the block to set a value" do
|
16
|
+
subject.add_to(proxy)
|
17
|
+
proxy.should have_received(:set).with(name, "value", false)
|
18
|
+
end
|
39
19
|
end
|
40
20
|
|
41
|
-
|
42
|
-
lambda {
|
43
|
-
|
44
|
-
|
21
|
+
context "with a block returning its block-level variable" do
|
22
|
+
let(:block) { lambda {|thing| thing } }
|
23
|
+
|
24
|
+
it "yields the proxy to the block" do
|
25
|
+
subject.add_to(proxy)
|
26
|
+
proxy.should have_received(:set).with(name, proxy, false)
|
27
|
+
end
|
45
28
|
end
|
46
29
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
30
|
+
context "with a block referencing an attribute on the proxy" do
|
31
|
+
let(:block) { lambda { attribute_defined_on_proxy } }
|
32
|
+
let(:result) { "other attribute value" }
|
33
|
+
|
34
|
+
before do
|
35
|
+
proxy.stubs(:attribute_defined_on_proxy => result)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "evaluates the attribute from the proxy" do
|
39
|
+
subject.add_to(proxy)
|
40
|
+
proxy.should have_received(:set).with(name, result, false)
|
41
|
+
end
|
55
42
|
end
|
56
43
|
|
57
|
-
|
58
|
-
|
44
|
+
context "with a block returning a sequence" do
|
45
|
+
let(:block) { lambda { Factory.sequence(:email) } }
|
46
|
+
|
47
|
+
it "raises a sequence abuse error" do
|
48
|
+
expect { subject.add_to(proxy) }.to raise_error(FactoryGirl::SequenceAbuseError)
|
49
|
+
end
|
59
50
|
end
|
60
51
|
end
|
52
|
+
|
53
|
+
describe FactoryGirl::Attribute::Dynamic, "with a string name" do
|
54
|
+
subject { FactoryGirl::Attribute::Dynamic.new("name", nil) }
|
55
|
+
its(:name) { should == :name }
|
56
|
+
end
|
@@ -1,50 +1,38 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Attribute::Implicit do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
end
|
4
|
+
let(:name) { :author }
|
5
|
+
let(:proxy) { stub("proxy") }
|
6
|
+
subject { FactoryGirl::Attribute::Implicit.new(name) }
|
8
7
|
|
9
|
-
|
10
|
-
@attr.name.should == @name
|
11
|
-
end
|
8
|
+
its(:name) { should == name }
|
12
9
|
|
13
10
|
context "with a known factory" do
|
14
11
|
before do
|
15
|
-
|
12
|
+
FactoryGirl.factories.stubs(:registered? => true)
|
16
13
|
end
|
17
14
|
|
18
|
-
it
|
19
|
-
proxy = "proxy"
|
20
|
-
stub(proxy).associate
|
21
|
-
@attr.add_to(proxy)
|
22
|
-
proxy.should have_received.associate(@name, @name, {})
|
23
|
-
end
|
15
|
+
it { should be_association }
|
24
16
|
|
25
|
-
|
26
|
-
@attr.should be_association
|
27
|
-
end
|
17
|
+
its(:factory) { should == name }
|
28
18
|
|
29
|
-
it "
|
30
|
-
|
19
|
+
it "associates the factory" do
|
20
|
+
proxy.stubs(:associate)
|
21
|
+
subject.add_to(proxy)
|
22
|
+
proxy.should have_received(:associate).with(name, name, {})
|
31
23
|
end
|
32
24
|
end
|
33
25
|
|
34
26
|
context "with a known sequence" do
|
35
|
-
|
36
|
-
|
37
|
-
end
|
27
|
+
let(:sequence) { FactoryGirl::Sequence.new(name, 1) { "magic" } }
|
28
|
+
before { FactoryGirl.register_sequence(sequence) }
|
38
29
|
|
39
|
-
it
|
40
|
-
proxy = "proxy"
|
41
|
-
stub(proxy).set
|
42
|
-
@attr.add_to(proxy)
|
43
|
-
proxy.should have_received.set(@name, "magic")
|
44
|
-
end
|
30
|
+
it { should_not be_association }
|
45
31
|
|
46
|
-
it "
|
47
|
-
|
32
|
+
it "generates the sequence" do
|
33
|
+
proxy.stubs(:set)
|
34
|
+
subject.add_to(proxy)
|
35
|
+
proxy.should have_received(:set).with(name, "magic")
|
48
36
|
end
|
49
37
|
end
|
50
38
|
end
|
@@ -1,21 +1,19 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Attribute::Sequence do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@attr = FactoryGirl::Attribute::Sequence.new(@name, @sequence)
|
9
|
-
end
|
4
|
+
let(:sequence_name) { :name }
|
5
|
+
let(:name) { :first_name }
|
6
|
+
let(:sequence) { FactoryGirl::Sequence.new(sequence_name, 5) { |n| "Name #{n}" } }
|
7
|
+
let(:proxy) { stub("proxy") }
|
10
8
|
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
subject { FactoryGirl::Attribute::Sequence.new(name, sequence_name) }
|
10
|
+
before { FactoryGirl.register_sequence(sequence) }
|
11
|
+
|
12
|
+
its(:name) { should == name }
|
14
13
|
|
15
14
|
it "assigns the next value in the sequence" do
|
16
|
-
proxy
|
17
|
-
|
18
|
-
|
19
|
-
proxy.should have_received.set(@name, "Name 5")
|
15
|
+
proxy.stubs(:set)
|
16
|
+
subject.add_to(proxy)
|
17
|
+
proxy.should have_received(:set).with(name, "Name 5")
|
20
18
|
end
|
21
19
|
end
|
@@ -1,29 +1,22 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FactoryGirl::Attribute::Static do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
@attr = FactoryGirl::Attribute::Static.new(@name, @value)
|
8
|
-
end
|
4
|
+
let(:name) { :first_name }
|
5
|
+
let(:value) { "John" }
|
6
|
+
let(:proxy) { stub("proxy") }
|
9
7
|
|
10
|
-
|
11
|
-
@attr.name.should == @name
|
12
|
-
end
|
8
|
+
subject { FactoryGirl::Attribute::Static.new(name, value) }
|
13
9
|
|
14
|
-
|
15
|
-
@proxy = "proxy"
|
16
|
-
mock(@proxy).set(@name, @value)
|
17
|
-
@attr.add_to(@proxy)
|
18
|
-
end
|
10
|
+
its(:name) { should == name }
|
19
11
|
|
20
|
-
it "
|
21
|
-
|
22
|
-
|
23
|
-
|
12
|
+
it "sets its static value on a proxy" do
|
13
|
+
proxy.stubs(:set)
|
14
|
+
subject.add_to(proxy)
|
15
|
+
proxy.should have_received(:set).with(name, value, false)
|
24
16
|
end
|
17
|
+
end
|
25
18
|
|
26
|
-
|
27
|
-
FactoryGirl::Attribute::Static.new(
|
28
|
-
|
19
|
+
describe FactoryGirl::Attribute::Static, "with a string name" do
|
20
|
+
subject { FactoryGirl::Attribute::Static.new("name", nil) }
|
21
|
+
its(:name) { should == :name }
|
29
22
|
end
|