factory_girl 2.1.2 → 2.2.0
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.
- data/Changelog +25 -0
- data/GETTING_STARTED.md +10 -3
- data/Gemfile.lock +1 -1
- data/README.md +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.rb +9 -0
- data/lib/factory_girl/attribute.rb +2 -6
- data/lib/factory_girl/attribute/association.rb +1 -1
- data/lib/factory_girl/attribute/dynamic.rb +8 -3
- data/lib/factory_girl/attribute/sequence.rb +8 -3
- data/lib/factory_girl/attribute/static.rb +7 -3
- data/lib/factory_girl/attribute_list.rb +5 -4
- data/lib/factory_girl/declaration.rb +5 -5
- data/lib/factory_girl/declaration/association.rb +1 -1
- data/lib/factory_girl/declaration/dynamic.rb +3 -3
- data/lib/factory_girl/declaration/implicit.rb +3 -3
- data/lib/factory_girl/declaration/static.rb +3 -3
- data/lib/factory_girl/definition_proxy.rb +14 -11
- data/lib/factory_girl/factory.rb +71 -79
- data/lib/factory_girl/proxy.rb +6 -1
- data/lib/factory_girl/proxy/attributes_for.rb +2 -6
- data/lib/factory_girl/proxy/build.rb +2 -7
- data/lib/factory_girl/proxy/stub.rb +2 -6
- data/lib/factory_girl/step_definitions.rb +3 -2
- data/lib/factory_girl/syntax/default.rb +1 -9
- data/lib/factory_girl/syntax/methods.rb +16 -8
- data/lib/factory_girl/syntax/vintage.rb +0 -3
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/attribute_existing_on_object.rb +21 -0
- data/spec/acceptance/attributes_for_spec.rb +17 -0
- data/spec/acceptance/build_spec.rb +17 -0
- data/spec/acceptance/build_stubbed_spec.rb +18 -0
- data/spec/acceptance/create_spec.rb +18 -0
- data/spec/acceptance/define_child_before_parent_spec.rb +21 -0
- data/spec/acceptance/syntax/blueprint_spec.rb +2 -2
- data/spec/acceptance/syntax/generate_spec.rb +9 -9
- data/spec/acceptance/syntax/make_spec.rb +6 -6
- data/spec/acceptance/syntax/sham_spec.rb +3 -3
- data/spec/acceptance/syntax/vintage_spec.rb +30 -29
- data/spec/acceptance/transient_attributes_spec.rb +45 -3
- data/spec/factory_girl/attribute/dynamic_spec.rb +5 -5
- data/spec/factory_girl/attribute/sequence_spec.rb +1 -1
- data/spec/factory_girl/attribute/static_spec.rb +3 -3
- data/spec/factory_girl/attribute_list_spec.rb +9 -24
- data/spec/factory_girl/attribute_spec.rb +3 -3
- data/spec/factory_girl/definition_proxy_spec.rb +23 -42
- data/spec/factory_girl/factory_spec.rb +26 -95
- data/spec/factory_girl/find_definitions_spec.rb +1 -1
- data/spec/support/shared_examples/proxy.rb +1 -1
- metadata +119 -122
data/lib/factory_girl/proxy.rb
CHANGED
@@ -5,12 +5,17 @@ module FactoryGirl
|
|
5
5
|
|
6
6
|
def initialize(klass)
|
7
7
|
@callbacks = {}
|
8
|
+
@ignored_attributes = {}
|
8
9
|
end
|
9
10
|
|
10
11
|
def get(attribute)
|
11
12
|
end
|
12
13
|
|
13
|
-
def set(attribute, value
|
14
|
+
def set(attribute, value)
|
15
|
+
end
|
16
|
+
|
17
|
+
def set_ignored(attribute, value)
|
18
|
+
@ignored_attributes[attribute] = value
|
14
19
|
end
|
15
20
|
|
16
21
|
def associate(name, factory, attributes)
|
@@ -11,12 +11,8 @@ module FactoryGirl
|
|
11
11
|
@ignored_attributes[attribute] || @hash[attribute]
|
12
12
|
end
|
13
13
|
|
14
|
-
def set(attribute, value
|
15
|
-
|
16
|
-
@ignored_attributes[attribute] = value
|
17
|
-
else
|
18
|
-
@hash[attribute] = value
|
19
|
-
end
|
14
|
+
def set(attribute, value)
|
15
|
+
@hash[attribute] = value
|
20
16
|
end
|
21
17
|
|
22
18
|
def result(to_create)
|
@@ -4,7 +4,6 @@ module FactoryGirl
|
|
4
4
|
def initialize(klass)
|
5
5
|
super(klass)
|
6
6
|
@instance = klass.new
|
7
|
-
@ignored_attributes = {}
|
8
7
|
end
|
9
8
|
|
10
9
|
def get(attribute)
|
@@ -15,12 +14,8 @@ module FactoryGirl
|
|
15
14
|
end
|
16
15
|
end
|
17
16
|
|
18
|
-
def set(attribute, value
|
19
|
-
|
20
|
-
@ignored_attributes[attribute] = value
|
21
|
-
else
|
22
|
-
@instance.send(:"#{attribute}=", value)
|
23
|
-
end
|
17
|
+
def set(attribute, value)
|
18
|
+
@instance.send(:"#{attribute}=", value)
|
24
19
|
end
|
25
20
|
|
26
21
|
def associate(name, factory_name, overrides)
|
@@ -55,12 +55,8 @@ module FactoryGirl
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
def set(attribute, value
|
59
|
-
|
60
|
-
@ignored_attributes[attribute] = value
|
61
|
-
else
|
62
|
-
@instance.send(:"#{attribute}=", value)
|
63
|
-
end
|
58
|
+
def set(attribute, value)
|
59
|
+
@instance.send(:"#{attribute}=", value)
|
64
60
|
end
|
65
61
|
|
66
62
|
def associate(name, factory_name, overrides)
|
@@ -95,6 +95,7 @@ end
|
|
95
95
|
World(FactoryGirlStepHelpers)
|
96
96
|
|
97
97
|
FactoryGirl.factories.each do |factory|
|
98
|
+
factory.ensure_compiled
|
98
99
|
factory.human_names.each do |human_name|
|
99
100
|
Given /^the following (?:#{human_name}|#{human_name.pluralize}) exists?:$/i do |table|
|
100
101
|
table.hashes.each do |human_hash|
|
@@ -108,7 +109,7 @@ FactoryGirl.factories.each do |factory|
|
|
108
109
|
end
|
109
110
|
|
110
111
|
Given /^(\d+) #{human_name.pluralize} exist$/i do |count|
|
111
|
-
|
112
|
+
FactoryGirl.create_list(factory.name, count.to_i)
|
112
113
|
end
|
113
114
|
|
114
115
|
if factory.build_class.respond_to?(:columns)
|
@@ -119,7 +120,7 @@ FactoryGirl.factories.each do |factory|
|
|
119
120
|
end
|
120
121
|
|
121
122
|
Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
|
122
|
-
|
123
|
+
FactoryGirl.create_list(factory.name, count.to_i, column.name => value)
|
123
124
|
end
|
124
125
|
end
|
125
126
|
end
|
@@ -21,14 +21,6 @@ module FactoryGirl
|
|
21
21
|
proxy = FactoryGirl::DefinitionProxy.new(factory)
|
22
22
|
proxy.instance_eval(&block) if block_given?
|
23
23
|
|
24
|
-
if traits = options.delete(:traits)
|
25
|
-
factory.inherit_traits(traits)
|
26
|
-
end
|
27
|
-
|
28
|
-
if parent = options.delete(:parent)
|
29
|
-
factory.inherit_factory(FactoryGirl.factory_by_name(parent))
|
30
|
-
end
|
31
|
-
|
32
24
|
FactoryGirl.register_factory(factory)
|
33
25
|
|
34
26
|
proxy.child_factories.each do |(child_name, child_options, child_block)|
|
@@ -54,7 +46,7 @@ module FactoryGirl
|
|
54
46
|
factory = FactoryGirl.factory_by_name(name).allow_overrides
|
55
47
|
proxy = FactoryGirl::DefinitionProxy.new(factory)
|
56
48
|
proxy.instance_eval(&block)
|
57
|
-
factory.
|
49
|
+
factory.ensure_compiled
|
58
50
|
end
|
59
51
|
end
|
60
52
|
end
|
@@ -10,12 +10,14 @@ module FactoryGirl
|
|
10
10
|
# The name of the factory that should be used.
|
11
11
|
# * overrides: +Hash+
|
12
12
|
# Attributes to overwrite for this set.
|
13
|
+
# * block:
|
14
|
+
# Yields the hash of attributes.
|
13
15
|
#
|
14
16
|
# Returns: +Hash+
|
15
17
|
# A set of attributes that can be used to build an instance of the class
|
16
18
|
# this factory generates.
|
17
|
-
def attributes_for(name, overrides = {})
|
18
|
-
FactoryGirl.factory_by_name(name).run(Proxy::AttributesFor, overrides)
|
19
|
+
def attributes_for(name, overrides = {}, &block)
|
20
|
+
FactoryGirl.factory_by_name(name).run(Proxy::AttributesFor, overrides, &block)
|
19
21
|
end
|
20
22
|
|
21
23
|
# Generates and returns an instance from this factory. Attributes can be
|
@@ -26,12 +28,14 @@ module FactoryGirl
|
|
26
28
|
# The name of the factory that should be used.
|
27
29
|
# * overrides: +Hash+
|
28
30
|
# Attributes to overwrite for this instance.
|
31
|
+
# * block:
|
32
|
+
# Yields the built instance.
|
29
33
|
#
|
30
34
|
# Returns: +Object+
|
31
35
|
# An instance of the class this factory generates, with generated attributes
|
32
36
|
# assigned.
|
33
|
-
def build(name, overrides = {})
|
34
|
-
FactoryGirl.factory_by_name(name).run(Proxy::Build, overrides)
|
37
|
+
def build(name, overrides = {}, &block)
|
38
|
+
FactoryGirl.factory_by_name(name).run(Proxy::Build, overrides, &block)
|
35
39
|
end
|
36
40
|
|
37
41
|
# Generates, saves, and returns an instance from this factory. Attributes can
|
@@ -46,12 +50,14 @@ module FactoryGirl
|
|
46
50
|
# The name of the factory that should be used.
|
47
51
|
# * overrides: +Hash+
|
48
52
|
# Attributes to overwrite for this instance.
|
53
|
+
# * block:
|
54
|
+
# Yields the created instance.
|
49
55
|
#
|
50
56
|
# Returns: +Object+
|
51
57
|
# A saved instance of the class this factory generates, with generated
|
52
58
|
# attributes assigned.
|
53
|
-
def create(name, overrides = {})
|
54
|
-
FactoryGirl.factory_by_name(name).run(Proxy::Create, overrides)
|
59
|
+
def create(name, overrides = {}, &block)
|
60
|
+
FactoryGirl.factory_by_name(name).run(Proxy::Create, overrides, &block)
|
55
61
|
end
|
56
62
|
|
57
63
|
# Generates and returns an object with all attributes from this factory
|
@@ -63,11 +69,13 @@ module FactoryGirl
|
|
63
69
|
# The name of the factory that should be used.
|
64
70
|
# * overrides: +Hash+
|
65
71
|
# Attributes to overwrite for this instance.
|
72
|
+
# * block
|
73
|
+
# Yields the stubbed object.
|
66
74
|
#
|
67
75
|
# Returns: +Object+
|
68
76
|
# An object with generated attributes stubbed out.
|
69
|
-
def build_stubbed(name, overrides = {})
|
70
|
-
FactoryGirl.factory_by_name(name).run(Proxy::Stub, overrides)
|
77
|
+
def build_stubbed(name, overrides = {}, &block)
|
78
|
+
FactoryGirl.factory_by_name(name).run(Proxy::Stub, overrides, &block)
|
71
79
|
end
|
72
80
|
|
73
81
|
# Builds and returns multiple instances from this factory as an array. Attributes can be
|
@@ -28,9 +28,6 @@ module FactoryGirl
|
|
28
28
|
factory = FactoryGirl::Factory.new(name, options)
|
29
29
|
proxy = FactoryGirl::DefinitionProxy.new(factory)
|
30
30
|
yield(proxy)
|
31
|
-
if parent = options.delete(:parent)
|
32
|
-
factory.inherit_factory(FactoryGirl.factory_by_name(parent))
|
33
|
-
end
|
34
31
|
FactoryGirl.register_factory(factory)
|
35
32
|
end
|
36
33
|
|
data/lib/factory_girl/version.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "declaring attributes on a Factory that are private methods on Object" do
|
4
|
+
before do
|
5
|
+
define_model("Website", :system => :boolean, :link => :string, :sleep => :integer)
|
6
|
+
|
7
|
+
FactoryGirl.define do
|
8
|
+
factory :website do
|
9
|
+
system false
|
10
|
+
link "http://example.com"
|
11
|
+
sleep 15
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
subject { FactoryGirl.build(:website, :sleep => -5) }
|
17
|
+
|
18
|
+
its(:system) { should == false }
|
19
|
+
its(:link) { should == "http://example.com" }
|
20
|
+
its(:sleep) { should == -5 }
|
21
|
+
end
|
@@ -44,3 +44,20 @@ describe "a generated attributes hash" do
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
+
describe "calling `attributes_for` with a block" do
|
48
|
+
include FactoryGirl::Syntax::Methods
|
49
|
+
|
50
|
+
before do
|
51
|
+
define_model('Company', :name => :string)
|
52
|
+
|
53
|
+
FactoryGirl.define do
|
54
|
+
factory :company
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "passes the hash of attributes" do
|
59
|
+
attributes_for(:company, :name => 'thoughtbot') do |attributes|
|
60
|
+
attributes[:name].should eq('thoughtbot')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -60,5 +60,22 @@ describe "a built instance with :method => :build" do
|
|
60
60
|
subject.user.should be_kind_of(User)
|
61
61
|
subject.user.should be_new_record
|
62
62
|
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "calling `build` with a block" do
|
66
|
+
include FactoryGirl::Syntax::Methods
|
63
67
|
|
68
|
+
before do
|
69
|
+
define_model('Company', :name => :string)
|
70
|
+
|
71
|
+
FactoryGirl.define do
|
72
|
+
factory :company
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "passes the built instance" do
|
77
|
+
build(:company, :name => 'thoughtbot') do |company|
|
78
|
+
company.name.should eq('thoughtbot')
|
79
|
+
end
|
80
|
+
end
|
64
81
|
end
|
@@ -75,3 +75,21 @@ describe "a generated stub instance" do
|
|
75
75
|
end
|
76
76
|
end
|
77
77
|
|
78
|
+
describe "calling `build_stubbed` with a block" do
|
79
|
+
include FactoryGirl::Syntax::Methods
|
80
|
+
|
81
|
+
before do
|
82
|
+
define_model('Company', :name => :string)
|
83
|
+
|
84
|
+
FactoryGirl.define do
|
85
|
+
factory :company
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
it "passes the stub instance" do
|
90
|
+
build_stubbed(:company, :name => 'thoughtbot') do |company|
|
91
|
+
company.name.should eq('thoughtbot')
|
92
|
+
expect { company.save }.to raise_error(RuntimeError)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -89,3 +89,21 @@ describe "a custom create" do
|
|
89
89
|
FactoryGirl.create(:user).should be_persisted
|
90
90
|
end
|
91
91
|
end
|
92
|
+
|
93
|
+
describe "calling `create` with a block" do
|
94
|
+
include FactoryGirl::Syntax::Methods
|
95
|
+
|
96
|
+
before do
|
97
|
+
define_model('Company', :name => :string)
|
98
|
+
|
99
|
+
FactoryGirl.define do
|
100
|
+
factory :company
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it "passes the created instance" do
|
105
|
+
create(:company, :name => 'thoughtbot') do |company|
|
106
|
+
company.name.should eq('thoughtbot')
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "defining a child factory before a parent" do
|
4
|
+
before do
|
5
|
+
define_model("User", :name => :string, :admin => :boolean, :email => :string, :upper_email => :string, :login => :string)
|
6
|
+
|
7
|
+
FactoryGirl.define do
|
8
|
+
factory :admin, :parent => :user do
|
9
|
+
admin true
|
10
|
+
end
|
11
|
+
|
12
|
+
factory :user do
|
13
|
+
name "awesome"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
it "creates admin factories correctly" do
|
19
|
+
FactoryGirl.create(:admin).should be_admin
|
20
|
+
end
|
21
|
+
end
|
@@ -19,11 +19,11 @@ describe "a blueprint" do
|
|
19
19
|
@instance = FactoryGirl.create(:user, :last_name => 'Rye')
|
20
20
|
end
|
21
21
|
|
22
|
-
it "
|
22
|
+
it "uses attributes from the blueprint" do
|
23
23
|
@instance.first_name.should == 'Bill'
|
24
24
|
end
|
25
25
|
|
26
|
-
it "
|
26
|
+
it "evaluates attribute blocks for each instance" do
|
27
27
|
@instance.email.should =~ /somebody\d+@example.com/
|
28
28
|
FactoryGirl.create(:user).email.should_not == @instance.email
|
29
29
|
end
|
@@ -17,16 +17,16 @@ describe "a factory using generate syntax" do
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
it "
|
21
|
-
|
20
|
+
it "does not raise an error when generating an invalid instance" do
|
21
|
+
expect { User.generate(:first_name => nil) }.to_not raise_error
|
22
22
|
end
|
23
23
|
|
24
|
-
it "
|
25
|
-
|
24
|
+
it "raises an error when forcefully generating an invalid instance" do
|
25
|
+
expect { User.generate!(:first_name => nil) }.to raise_error(ActiveRecord::RecordInvalid)
|
26
26
|
end
|
27
27
|
|
28
28
|
%w(generate generate! spawn).each do |method|
|
29
|
-
it "
|
29
|
+
it "yields a generated instance when using #{method} with a block" do
|
30
30
|
saved_instance = nil
|
31
31
|
User.send(method) {|instance| saved_instance = instance }
|
32
32
|
saved_instance.should be_kind_of(User)
|
@@ -37,20 +37,20 @@ describe "a factory using generate syntax" do
|
|
37
37
|
@instance = User.send(method, :last_name => 'Rye')
|
38
38
|
end
|
39
39
|
|
40
|
-
it "
|
40
|
+
it "uses attributes from the factory" do
|
41
41
|
@instance.first_name.should == 'Bill'
|
42
42
|
end
|
43
43
|
|
44
|
-
it "
|
44
|
+
it "uses attributes passed to generate" do
|
45
45
|
@instance.last_name.should == 'Rye'
|
46
46
|
end
|
47
47
|
|
48
48
|
if method == 'spawn'
|
49
|
-
it "
|
49
|
+
it "does not save the record" do
|
50
50
|
@instance.should be_new_record
|
51
51
|
end
|
52
52
|
else
|
53
|
-
it "
|
53
|
+
it "does save the record" do
|
54
54
|
@instance.should_not be_new_record
|
55
55
|
end
|
56
56
|
end
|
@@ -19,15 +19,15 @@ describe "a factory using make syntax" do
|
|
19
19
|
@instance = User.make(:last_name => 'Rye')
|
20
20
|
end
|
21
21
|
|
22
|
-
it "
|
22
|
+
it "uses attributes from the factory" do
|
23
23
|
@instance.first_name.should == 'Bill'
|
24
24
|
end
|
25
25
|
|
26
|
-
it "
|
26
|
+
it "uses attributes passed to make" do
|
27
27
|
@instance.last_name.should == 'Rye'
|
28
28
|
end
|
29
29
|
|
30
|
-
it "
|
30
|
+
it "builds the record" do
|
31
31
|
@instance.should be_new_record
|
32
32
|
end
|
33
33
|
end
|
@@ -37,15 +37,15 @@ describe "a factory using make syntax" do
|
|
37
37
|
@instance = User.make!(:last_name => 'Rye')
|
38
38
|
end
|
39
39
|
|
40
|
-
it "
|
40
|
+
it "uses attributes from the factory" do
|
41
41
|
@instance.first_name.should == 'Bill'
|
42
42
|
end
|
43
43
|
|
44
|
-
it "
|
44
|
+
it "uses attributes passed to make" do
|
45
45
|
@instance.last_name.should == 'Rye'
|
46
46
|
end
|
47
47
|
|
48
|
-
it "
|
48
|
+
it "saves the record" do
|
49
49
|
@instance.should_not be_new_record
|
50
50
|
end
|
51
51
|
end
|
@@ -28,15 +28,15 @@ describe "a factory using sham syntax" do
|
|
28
28
|
@instance = FactoryGirl.create(:user, :last_name => 'Rye')
|
29
29
|
end
|
30
30
|
|
31
|
-
it "
|
31
|
+
it "supports a sham called 'name'" do
|
32
32
|
@instance.first_name.should == 'Name'
|
33
33
|
end
|
34
34
|
|
35
|
-
it "
|
35
|
+
it "supports shams with starting values" do
|
36
36
|
@instance.username.should == 'User-FOO'
|
37
37
|
end
|
38
38
|
|
39
|
-
it "
|
39
|
+
it "uses the sham for the email" do
|
40
40
|
@instance.email.should =~ /somebody\d@example.com/
|
41
41
|
end
|
42
42
|
end
|