factory_girl 2.0.4 → 2.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. data/CONTRIBUTION_GUIDELINES.md +1 -0
  2. data/GETTING_STARTED.md +28 -4
  3. data/Gemfile +2 -1
  4. data/Gemfile.lock +6 -2
  5. data/features/factory_girl_steps.feature +17 -0
  6. data/features/step_definitions/database_steps.rb +22 -0
  7. data/features/support/factories.rb +13 -0
  8. data/features/support/test.db +0 -0
  9. data/lib/factory_girl/aliases.rb +1 -3
  10. data/lib/factory_girl/attribute.rb +16 -8
  11. data/lib/factory_girl/attribute/association.rb +0 -3
  12. data/lib/factory_girl/attribute/callback.rb +0 -2
  13. data/lib/factory_girl/attribute/dynamic.rb +1 -3
  14. data/lib/factory_girl/attribute/static.rb +1 -1
  15. data/lib/factory_girl/attribute_list.rb +30 -9
  16. data/lib/factory_girl/factory.rb +1 -1
  17. data/lib/factory_girl/proxy.rb +7 -5
  18. data/lib/factory_girl/proxy/attributes_for.rb +8 -3
  19. data/lib/factory_girl/proxy/build.rb +12 -3
  20. data/lib/factory_girl/proxy/stub.rb +12 -3
  21. data/lib/factory_girl/sequence.rb +2 -2
  22. data/lib/factory_girl/step_definitions.rb +3 -2
  23. data/lib/factory_girl/version.rb +1 -1
  24. data/spec/acceptance/attribute_aliases_spec.rb +0 -1
  25. data/spec/acceptance/attributes_for_spec.rb +0 -1
  26. data/spec/acceptance/attributes_ordered_spec.rb +24 -6
  27. data/spec/acceptance/build_list_spec.rb +0 -1
  28. data/spec/acceptance/build_spec.rb +0 -1
  29. data/spec/acceptance/build_stubbed_spec.rb +0 -1
  30. data/spec/acceptance/callbacks_spec.rb +0 -1
  31. data/spec/acceptance/create_list_spec.rb +0 -1
  32. data/spec/acceptance/create_spec.rb +0 -1
  33. data/spec/acceptance/default_strategy_spec.rb +0 -1
  34. data/spec/acceptance/definition_spec.rb +0 -1
  35. data/spec/acceptance/definition_without_block_spec.rb +0 -1
  36. data/spec/acceptance/overrides_spec.rb +0 -1
  37. data/spec/acceptance/parent_spec.rb +18 -1
  38. data/spec/acceptance/sequence_spec.rb +0 -1
  39. data/spec/acceptance/syntax/blueprint_spec.rb +0 -1
  40. data/spec/acceptance/syntax/generate_spec.rb +0 -1
  41. data/spec/acceptance/syntax/make_spec.rb +0 -1
  42. data/spec/acceptance/syntax/sham_spec.rb +0 -1
  43. data/spec/acceptance/syntax/vintage_spec.rb +27 -29
  44. data/spec/acceptance/traits_spec.rb +0 -1
  45. data/spec/acceptance/transient_attributes_spec.rb +68 -0
  46. data/spec/factory_girl/aliases_spec.rb +19 -21
  47. data/spec/factory_girl/attribute/association_spec.rb +16 -24
  48. data/spec/factory_girl/attribute/callback_spec.rb +14 -15
  49. data/spec/factory_girl/attribute/dynamic_spec.rb +41 -45
  50. data/spec/factory_girl/attribute/implicit_spec.rb +18 -30
  51. data/spec/factory_girl/attribute/sequence_spec.rb +11 -13
  52. data/spec/factory_girl/attribute/static_spec.rb +13 -20
  53. data/spec/factory_girl/attribute_list_spec.rb +9 -1
  54. data/spec/factory_girl/attribute_spec.rb +17 -28
  55. data/spec/factory_girl/definition_proxy_spec.rb +131 -82
  56. data/spec/factory_girl/deprecated_spec.rb +15 -36
  57. data/spec/factory_girl/factory_spec.rb +106 -135
  58. data/spec/factory_girl/find_definitions_spec.rb +7 -6
  59. data/spec/factory_girl/proxy/attributes_for_spec.rb +23 -32
  60. data/spec/factory_girl/proxy/build_spec.rb +6 -80
  61. data/spec/factory_girl/proxy/create_spec.rb +24 -89
  62. data/spec/factory_girl/proxy/stub_spec.rb +14 -73
  63. data/spec/factory_girl/proxy_spec.rb +53 -53
  64. data/spec/factory_girl/registry_spec.rb +13 -29
  65. data/spec/factory_girl/sequence_spec.rb +24 -72
  66. data/spec/factory_girl_spec.rb +10 -5
  67. data/spec/spec_helper.rb +5 -79
  68. data/spec/support/macros/define_constant.rb +86 -0
  69. data/spec/support/shared_examples/proxy.rb +99 -0
  70. metadata +34 -20
  71. data/spec/acceptance/acceptance_helper.rb +0 -11
@@ -1,5 +1,4 @@
1
1
  require "spec_helper"
2
- require "acceptance/acceptance_helper"
3
2
 
4
3
  describe "an instance generated by a factory with multiple traits" do
5
4
  before do
@@ -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 Factory, "aliases" do
4
-
5
- it "should include an attribute as an alias for itself by default" do
6
- FactoryGirl.aliases_for(:test).should include(:test)
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
- it "should include the root of a foreign key as an alias by default" do
10
- FactoryGirl.aliases_for(:test_id).should include(:test)
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
- it "should include an attribute's foreign key as an alias by default" do
14
- FactoryGirl.aliases_for(:test).should include(:test_id)
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
- it "should NOT include an attribute as an alias when it starts with underscore" do
18
- FactoryGirl.aliases_for(:_id).should_not include(:id)
22
+ describe Factory, "after defining an alias" do
23
+ before do
24
+ Factory.alias(/(.*)_suffix/, '\1')
19
25
  end
20
26
 
21
- describe "after adding an alias" do
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
- before do
5
- @name = :author
6
- @factory = :user
7
- @overrides = { :first_name => 'John' }
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
- it "should have a name" do
12
- @attr.name.should == @name
13
- end
9
+ subject { FactoryGirl::Attribute::Association.new(name, factory, overrides) }
14
10
 
15
- it "is an association" do
16
- @attr.should be_association
17
- end
11
+ it { should be_association }
12
+ its(:name) { should == name }
13
+ its(:factory) { should == factory }
18
14
 
19
- it "should have a factory" do
20
- @attr.factory.should == @factory
21
- end
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
- it "should convert names to symbols" do
31
- FactoryGirl::Attribute::Association.new('name', :user, {}).name.should == :name
32
- end
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
- before do
5
- @name = :after_create
6
- @block = proc{ 'block' }
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
- it "should have a name" do
11
- @attr.name.should == @name
12
- end
8
+ subject { FactoryGirl::Attribute::Callback.new(name, block) }
13
9
 
14
- it "should set its callback on a proxy" do
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 "should convert names to symbols" do
21
- FactoryGirl::Attribute::Callback.new('name', nil).name.should == :name
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
- before do
5
- @name = :first_name
6
- @block = lambda { 'value' }
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
- it "should have a name" do
11
- @attr.name.should == @name
12
- end
8
+ subject { FactoryGirl::Attribute::Dynamic.new(name, block) }
13
9
 
14
- it "should call the block to set a value" do
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
- it "should yield the proxy to the block when adding its value to a proxy" do
22
- @block = lambda {|a| a }
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
- it "evaluates the block with in the context of the proxy without an argument" do
31
- result = 'other attribute value'
32
- @block = lambda { other_attribute }
33
- @attr = FactoryGirl::Attribute::Dynamic.new(:user, @block)
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
- it "should raise an error when defining an attribute writer" do
42
- lambda {
43
- FactoryGirl::Attribute::Dynamic.new('test=', nil)
44
- }.should raise_error(FactoryGirl::AttributeDefinitionError)
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
- it "should raise an error when returning a sequence" do
48
- stub(Factory).sequence { FactoryGirl::Sequence.new(:email) }
49
- block = lambda { Factory.sequence(:email) }
50
- attr = FactoryGirl::Attribute::Dynamic.new(:email, block)
51
- proxy = stub!.set.subject
52
- lambda {
53
- attr.add_to(proxy)
54
- }.should raise_error(FactoryGirl::SequenceAbuseError)
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
- it "should convert names to symbols" do
58
- FactoryGirl::Attribute::Dynamic.new('name', nil).name.should == :name
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
- before do
5
- @name = :author
6
- @attr = FactoryGirl::Attribute::Implicit.new(@name)
7
- end
4
+ let(:name) { :author }
5
+ let(:proxy) { stub("proxy") }
6
+ subject { FactoryGirl::Attribute::Implicit.new(name) }
8
7
 
9
- it "has a name" do
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
- stub(FactoryGirl.factories).registered? { true }
12
+ FactoryGirl.factories.stubs(:registered? => true)
16
13
  end
17
14
 
18
- it "associates the factory" do
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
- it "is an association" do
26
- @attr.should be_association
27
- end
17
+ its(:factory) { should == name }
28
18
 
29
- it "has a factory" do
30
- @attr.factory.should == @name
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
- before do
36
- FactoryGirl.register_sequence(FactoryGirl::Sequence.new(@name, 1) { "magic" })
37
- end
27
+ let(:sequence) { FactoryGirl::Sequence.new(name, 1) { "magic" } }
28
+ before { FactoryGirl.register_sequence(sequence) }
38
29
 
39
- it "generates the sequence" do
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 "isn't an association" do
47
- @attr.should_not be_association
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
- before do
5
- @name = :first_name
6
- @sequence = :name
7
- FactoryGirl.register_sequence(FactoryGirl::Sequence.new(@sequence, 5) { |n| "Name #{n}" })
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
- it "should have a name" do
12
- @attr.name.should == @name
13
- end
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 = "proxy"
17
- stub(proxy).set
18
- @attr.add_to(proxy)
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
- before do
5
- @name = :first_name
6
- @value = 'John'
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
- it "should have a name" do
11
- @attr.name.should == @name
12
- end
8
+ subject { FactoryGirl::Attribute::Static.new(name, value) }
13
9
 
14
- it "should set its static value on a proxy" do
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 "should raise an error when defining an attribute writer" do
21
- lambda {
22
- FactoryGirl::Attribute::Static.new('test=', nil)
23
- }.should raise_error(FactoryGirl::AttributeDefinitionError)
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
- it "should convert names to symbols" do
27
- FactoryGirl::Attribute::Static.new('name', nil).name.should == :name
28
- end
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