factory_girl 2.6.0 → 2.6.1
Sign up to get free protection for your applications and to get access to all the features.
- 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 +6 -1
- data/lib/factory_girl/callback.rb +2 -4
- data/lib/factory_girl/definition_proxy.rb +2 -12
- data/lib/factory_girl/evaluator.rb +4 -1
- data/lib/factory_girl/factory_runner.rb +26 -0
- data/lib/factory_girl/strategy_calculator.rb +29 -0
- data/lib/factory_girl/syntax/generate.rb +3 -6
- data/lib/factory_girl/syntax/make.rb +2 -2
- data/lib/factory_girl/syntax/methods.rb +4 -22
- data/lib/factory_girl/version.rb +1 -1
- data/spec/factory_girl/callback_spec.rb +1 -1
- data/spec/factory_girl/strategy_calculator_spec.rb +33 -0
- data/spec/spec_helper.rb +1 -3
- data/spec/support/shared_examples/strategy.rb +4 -4
- metadata +30 -29
- data/lib/factory_girl/association_runner.rb +0 -50
- data/spec/factory_girl/association_runner_spec.rb +0 -31
data/Gemfile.lock
CHANGED
data/gemfiles/2.3.gemfile.lock
CHANGED
data/gemfiles/3.0.gemfile.lock
CHANGED
data/gemfiles/3.1.gemfile.lock
CHANGED
data/gemfiles/3.2.gemfile.lock
CHANGED
data/lib/factory_girl.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require "active_support/core_ext/module/delegation"
|
2
2
|
|
3
3
|
require 'factory_girl/errors'
|
4
|
-
require 'factory_girl/
|
4
|
+
require 'factory_girl/factory_runner'
|
5
|
+
require 'factory_girl/strategy_calculator'
|
5
6
|
require 'factory_girl/strategy'
|
6
7
|
require 'factory_girl/registry'
|
7
8
|
require 'factory_girl/null_factory'
|
@@ -66,4 +67,8 @@ module FactoryGirl
|
|
66
67
|
def self.trait_by_name(name)
|
67
68
|
traits.find(name)
|
68
69
|
end
|
70
|
+
|
71
|
+
def self.callback_names
|
72
|
+
[:after_build, :after_create, :after_stub].freeze
|
73
|
+
end
|
69
74
|
end
|
@@ -1,7 +1,5 @@
|
|
1
1
|
module FactoryGirl
|
2
2
|
class Callback
|
3
|
-
VALID_NAMES = [:after_build, :after_create, :after_stub].freeze
|
4
|
-
|
5
3
|
attr_reader :name, :block
|
6
4
|
|
7
5
|
def initialize(name, block)
|
@@ -26,9 +24,9 @@ module FactoryGirl
|
|
26
24
|
private
|
27
25
|
|
28
26
|
def check_name
|
29
|
-
unless
|
27
|
+
unless FactoryGirl.callback_names.include?(name)
|
30
28
|
raise InvalidCallbackNameError, "#{name} is not a valid callback name. " +
|
31
|
-
"Valid callback names are #{
|
29
|
+
"Valid callback names are #{FactoryGirl.callback_names.inspect}"
|
32
30
|
end
|
33
31
|
end
|
34
32
|
end
|
@@ -85,6 +85,8 @@ module FactoryGirl
|
|
85
85
|
@definition.declare_attribute(Declaration::Implicit.new(name, @definition, @ignore))
|
86
86
|
elsif args.first.is_a?(Hash) && args.first.has_key?(:factory)
|
87
87
|
association(name, *args)
|
88
|
+
elsif FactoryGirl.callback_names.include?(name)
|
89
|
+
@definition.add_callback(Callback.new(name, block))
|
88
90
|
else
|
89
91
|
add_attribute(name, *args, &block)
|
90
92
|
end
|
@@ -138,18 +140,6 @@ module FactoryGirl
|
|
138
140
|
@definition.declare_attribute(Declaration::Association.new(name, options))
|
139
141
|
end
|
140
142
|
|
141
|
-
def after_build(&block)
|
142
|
-
@definition.add_callback(Callback.new(:after_build, block))
|
143
|
-
end
|
144
|
-
|
145
|
-
def after_create(&block)
|
146
|
-
@definition.add_callback(Callback.new(:after_create, block))
|
147
|
-
end
|
148
|
-
|
149
|
-
def after_stub(&block)
|
150
|
-
@definition.add_callback(Callback.new(:after_stub, block))
|
151
|
-
end
|
152
|
-
|
153
143
|
def to_create(&block)
|
154
144
|
@definition.to_create(&block)
|
155
145
|
end
|
@@ -36,9 +36,12 @@ module FactoryGirl
|
|
36
36
|
overrides[:method]
|
37
37
|
elsif overrides.has_key?(:strategy)
|
38
38
|
overrides[:strategy]
|
39
|
+
else
|
40
|
+
Strategy::Create
|
39
41
|
end
|
40
42
|
|
41
|
-
|
43
|
+
build_strategy = StrategyCalculator.new(build_strategy).strategy
|
44
|
+
runner = FactoryRunner.new(factory_name, build_strategy, [overrides.except(:method, :strategy)])
|
42
45
|
@build_strategy.association(runner)
|
43
46
|
end
|
44
47
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class FactoryRunner
|
3
|
+
def initialize(name, strategy, traits_and_overrides)
|
4
|
+
@name = name
|
5
|
+
@strategy = strategy
|
6
|
+
|
7
|
+
@overrides = if traits_and_overrides.last.respond_to?(:has_key?)
|
8
|
+
traits_and_overrides.pop
|
9
|
+
else
|
10
|
+
{}
|
11
|
+
end
|
12
|
+
@traits = traits_and_overrides
|
13
|
+
end
|
14
|
+
|
15
|
+
def run(strategy_override = nil, &block)
|
16
|
+
strategy_override ||= @strategy
|
17
|
+
factory = FactoryGirl.factory_by_name(@name)
|
18
|
+
|
19
|
+
if @traits.any?
|
20
|
+
factory = factory.with_traits(@traits)
|
21
|
+
end
|
22
|
+
|
23
|
+
factory.run(strategy_override, @overrides, &block)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module FactoryGirl
|
2
|
+
class StrategyCalculator
|
3
|
+
def initialize(name_or_object)
|
4
|
+
@name_or_object = name_or_object
|
5
|
+
end
|
6
|
+
|
7
|
+
def strategy
|
8
|
+
if strategy_is_object?
|
9
|
+
@name_or_object
|
10
|
+
else
|
11
|
+
strategy_name_to_object
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def strategy_is_object?
|
18
|
+
@name_or_object.is_a?(Class) && @name_or_object.ancestors.include?(::FactoryGirl::Strategy)
|
19
|
+
end
|
20
|
+
|
21
|
+
def strategy_name_to_object
|
22
|
+
case @name_or_object
|
23
|
+
when :build then Strategy::Build
|
24
|
+
when :create then Strategy::Create
|
25
|
+
else raise "unrecognized method #{@name_or_object}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -42,23 +42,20 @@ module FactoryGirl
|
|
42
42
|
module ClassMethods #:nodoc:
|
43
43
|
|
44
44
|
def generate(overrides = {}, &block)
|
45
|
-
|
46
|
-
instance = factory.run(Strategy::Build, overrides)
|
45
|
+
instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
|
47
46
|
instance.save
|
48
47
|
yield(instance) if block_given?
|
49
48
|
instance
|
50
49
|
end
|
51
50
|
|
52
51
|
def generate!(overrides = {}, &block)
|
53
|
-
|
54
|
-
instance = factory.run(Strategy::Create, overrides)
|
52
|
+
instance = FactoryRunner.new(name.underscore, Strategy::Create, [overrides]).run
|
55
53
|
yield(instance) if block_given?
|
56
54
|
instance
|
57
55
|
end
|
58
56
|
|
59
57
|
def spawn(overrides = {}, &block)
|
60
|
-
|
61
|
-
instance = factory.run(Strategy::Build, overrides)
|
58
|
+
instance = FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
|
62
59
|
yield(instance) if block_given?
|
63
60
|
instance
|
64
61
|
end
|
@@ -28,11 +28,11 @@ module FactoryGirl
|
|
28
28
|
module ClassMethods #:nodoc:
|
29
29
|
|
30
30
|
def make(overrides = {})
|
31
|
-
|
31
|
+
FactoryRunner.new(name.underscore, Strategy::Build, [overrides]).run
|
32
32
|
end
|
33
33
|
|
34
34
|
def make!(overrides = {})
|
35
|
-
|
35
|
+
FactoryRunner.new(name.underscore, Strategy::Create, [overrides]).run
|
36
36
|
end
|
37
37
|
|
38
38
|
end
|
@@ -18,7 +18,7 @@ module FactoryGirl
|
|
18
18
|
# A set of attributes that can be used to build an instance of the class
|
19
19
|
# this factory generates.
|
20
20
|
def attributes_for(name, *traits_and_overrides, &block)
|
21
|
-
|
21
|
+
FactoryRunner.new(name, Strategy::AttributesFor, traits_and_overrides).run(&block)
|
22
22
|
end
|
23
23
|
|
24
24
|
# Generates and returns an instance from this factory. Attributes can be
|
@@ -37,7 +37,7 @@ module FactoryGirl
|
|
37
37
|
# An instance of the class this factory generates, with generated attributes
|
38
38
|
# assigned.
|
39
39
|
def build(name, *traits_and_overrides, &block)
|
40
|
-
|
40
|
+
FactoryRunner.new(name, Strategy::Build, traits_and_overrides).run(&block)
|
41
41
|
end
|
42
42
|
|
43
43
|
# Generates, saves, and returns an instance from this factory. Attributes can
|
@@ -60,7 +60,7 @@ module FactoryGirl
|
|
60
60
|
# A saved instance of the class this factory generates, with generated
|
61
61
|
# attributes assigned.
|
62
62
|
def create(name, *traits_and_overrides, &block)
|
63
|
-
|
63
|
+
FactoryRunner.new(name, Strategy::Create, traits_and_overrides).run(&block)
|
64
64
|
end
|
65
65
|
|
66
66
|
# Generates and returns an object with all attributes from this factory
|
@@ -79,7 +79,7 @@ module FactoryGirl
|
|
79
79
|
# Returns: +Object+
|
80
80
|
# An object with generated attributes stubbed out.
|
81
81
|
def build_stubbed(name, *traits_and_overrides, &block)
|
82
|
-
|
82
|
+
FactoryRunner.new(name, Strategy::Stub, traits_and_overrides).run(&block)
|
83
83
|
end
|
84
84
|
|
85
85
|
# Builds and returns multiple instances from this factory as an array. Attributes can be
|
@@ -131,24 +131,6 @@ module FactoryGirl
|
|
131
131
|
def generate(name)
|
132
132
|
FactoryGirl.sequence_by_name(name).next
|
133
133
|
end
|
134
|
-
|
135
|
-
private
|
136
|
-
|
137
|
-
def run_factory_girl_strategy(name, traits_and_overrides, strategy, &block)
|
138
|
-
overrides = if traits_and_overrides.last.respond_to?(:has_key?)
|
139
|
-
traits_and_overrides.pop
|
140
|
-
else
|
141
|
-
{}
|
142
|
-
end
|
143
|
-
|
144
|
-
factory = FactoryGirl.factory_by_name(name)
|
145
|
-
|
146
|
-
if traits_and_overrides.any?
|
147
|
-
factory = factory.with_traits(traits_and_overrides)
|
148
|
-
end
|
149
|
-
|
150
|
-
factory.run(strategy, overrides, &block)
|
151
|
-
end
|
152
134
|
end
|
153
135
|
end
|
154
136
|
end
|
data/lib/factory_girl/version.rb
CHANGED
@@ -28,7 +28,7 @@ describe FactoryGirl::Callback do
|
|
28
28
|
end
|
29
29
|
|
30
30
|
it "allows valid callback names to be assigned" do
|
31
|
-
FactoryGirl
|
31
|
+
FactoryGirl.callback_names.each do |callback_name|
|
32
32
|
expect { FactoryGirl::Callback.new(callback_name, lambda {}) }.
|
33
33
|
to_not raise_error(FactoryGirl::InvalidCallbackNameError)
|
34
34
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe FactoryGirl::StrategyCalculator, "with a FactoryGirl::Strategy object" do
|
4
|
+
let(:strategy) { FactoryGirl::Strategy::Build }
|
5
|
+
|
6
|
+
it "returns the strategy object" do
|
7
|
+
FactoryGirl::StrategyCalculator.new(strategy).strategy.should == strategy
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe FactoryGirl::StrategyCalculator, "with a non-FactoryGirl::Strategy object" do
|
12
|
+
before { define_class "MyAwesomeStrategy" }
|
13
|
+
|
14
|
+
let(:strategy) { MyAwesomeStrategy }
|
15
|
+
|
16
|
+
it "returns the strategy object" do
|
17
|
+
expect { FactoryGirl::StrategyCalculator.new(strategy).strategy }.to raise_error "unrecognized method MyAwesomeStrategy"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe FactoryGirl::StrategyCalculator do
|
22
|
+
it "returns the correct strategy object for :build" do
|
23
|
+
FactoryGirl::StrategyCalculator.new(:build).strategy.should == FactoryGirl::Strategy::Build
|
24
|
+
end
|
25
|
+
|
26
|
+
it "returns the correct strategy object for :create" do
|
27
|
+
FactoryGirl::StrategyCalculator.new(:create).strategy.should == FactoryGirl::Strategy::Create
|
28
|
+
end
|
29
|
+
|
30
|
+
it "raises when passing a bogus strategy" do
|
31
|
+
expect { FactoryGirl::StrategyCalculator.new(:bogus_strategy).strategy }.to raise_error "unrecognized method bogus_strategy"
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,7 +2,7 @@ shared_examples_for "strategy without association support" do
|
|
2
2
|
let(:attribute) { FactoryGirl::Attribute::Association.new(:user, :user, {}) }
|
3
3
|
|
4
4
|
def association_named(name, overrides)
|
5
|
-
runner = FactoryGirl::
|
5
|
+
runner = FactoryGirl::FactoryRunner.new(name, FactoryGirl::Strategy::Build, [overrides])
|
6
6
|
subject.association(runner)
|
7
7
|
end
|
8
8
|
|
@@ -21,7 +21,7 @@ shared_examples_for "strategy with association support" do |factory_girl_strateg
|
|
21
21
|
let(:factory) { stub("associate_factory") }
|
22
22
|
|
23
23
|
def association_named(name, strategy, overrides)
|
24
|
-
runner = FactoryGirl::
|
24
|
+
runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides])
|
25
25
|
subject.association(runner)
|
26
26
|
end
|
27
27
|
|
@@ -45,8 +45,8 @@ shared_examples_for "strategy with :strategy => :build" do |factory_girl_strateg
|
|
45
45
|
let(:factory) { stub("associate_factory") }
|
46
46
|
|
47
47
|
def association_named(name, overrides)
|
48
|
-
strategy = overrides[:strategy] || overrides[:method]
|
49
|
-
runner = FactoryGirl::
|
48
|
+
strategy = FactoryGirl::StrategyCalculator.new(overrides[:strategy] || overrides[:method]).strategy
|
49
|
+
runner = FactoryGirl::FactoryRunner.new(name, strategy, [overrides.except(:strategy, :method)])
|
50
50
|
subject.association(runner)
|
51
51
|
end
|
52
52
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: factory_girl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.6.
|
4
|
+
version: 2.6.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-02
|
12
|
+
date: 2012-03-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70301653289480 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.3.9
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70301653289480
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &70301653288800 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70301653288800
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: cucumber
|
38
|
-
requirement: &
|
38
|
+
requirement: &70301653287980 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70301653287980
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: timecop
|
49
|
-
requirement: &
|
49
|
+
requirement: &70301653285420 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70301653285420
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rcov
|
60
|
-
requirement: &
|
60
|
+
requirement: &70301653284860 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70301653284860
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: aruba
|
71
|
-
requirement: &
|
71
|
+
requirement: &70301653284360 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70301653284360
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: mocha
|
82
|
-
requirement: &
|
82
|
+
requirement: &70301653283880 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70301653283880
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: bourne
|
93
|
-
requirement: &
|
93
|
+
requirement: &70301653283400 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70301653283400
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: appraisal
|
104
|
-
requirement: &
|
104
|
+
requirement: &70301653282780 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ~>
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: 0.3.8
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *70301653282780
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: sqlite3-ruby
|
115
|
-
requirement: &
|
115
|
+
requirement: &70301653282340 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ! '>='
|
@@ -120,10 +120,10 @@ dependencies:
|
|
120
120
|
version: '0'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *70301653282340
|
124
124
|
- !ruby/object:Gem::Dependency
|
125
125
|
name: yard
|
126
|
-
requirement: &
|
126
|
+
requirement: &70301653281820 !ruby/object:Gem::Requirement
|
127
127
|
none: false
|
128
128
|
requirements:
|
129
129
|
- - ! '>='
|
@@ -131,10 +131,10 @@ dependencies:
|
|
131
131
|
version: '0'
|
132
132
|
type: :development
|
133
133
|
prerelease: false
|
134
|
-
version_requirements: *
|
134
|
+
version_requirements: *70301653281820
|
135
135
|
- !ruby/object:Gem::Dependency
|
136
136
|
name: bluecloth
|
137
|
-
requirement: &
|
137
|
+
requirement: &70301653280800 !ruby/object:Gem::Requirement
|
138
138
|
none: false
|
139
139
|
requirements:
|
140
140
|
- - ! '>='
|
@@ -142,7 +142,7 @@ dependencies:
|
|
142
142
|
version: '0'
|
143
143
|
type: :development
|
144
144
|
prerelease: false
|
145
|
-
version_requirements: *
|
145
|
+
version_requirements: *70301653280800
|
146
146
|
description: ! "factory_girl provides a framework and DSL for defining and\n using
|
147
147
|
factories - less error-prone, more explicit, and\n all-around
|
148
148
|
easier to work with than fixtures."
|
@@ -183,7 +183,6 @@ files:
|
|
183
183
|
- gemfiles/3.2.gemfile.lock
|
184
184
|
- lib/factory_girl.rb
|
185
185
|
- lib/factory_girl/aliases.rb
|
186
|
-
- lib/factory_girl/association_runner.rb
|
187
186
|
- lib/factory_girl/attribute.rb
|
188
187
|
- lib/factory_girl/attribute/association.rb
|
189
188
|
- lib/factory_girl/attribute/dynamic.rb
|
@@ -205,6 +204,7 @@ files:
|
|
205
204
|
- lib/factory_girl/evaluator.rb
|
206
205
|
- lib/factory_girl/evaluator_class_definer.rb
|
207
206
|
- lib/factory_girl/factory.rb
|
207
|
+
- lib/factory_girl/factory_runner.rb
|
208
208
|
- lib/factory_girl/find_definitions.rb
|
209
209
|
- lib/factory_girl/null_factory.rb
|
210
210
|
- lib/factory_girl/null_object.rb
|
@@ -218,6 +218,7 @@ files:
|
|
218
218
|
- lib/factory_girl/strategy/build.rb
|
219
219
|
- lib/factory_girl/strategy/create.rb
|
220
220
|
- lib/factory_girl/strategy/stub.rb
|
221
|
+
- lib/factory_girl/strategy_calculator.rb
|
221
222
|
- lib/factory_girl/syntax.rb
|
222
223
|
- lib/factory_girl/syntax/blueprint.rb
|
223
224
|
- lib/factory_girl/syntax/default.rb
|
@@ -259,7 +260,6 @@ files:
|
|
259
260
|
- spec/acceptance/traits_spec.rb
|
260
261
|
- spec/acceptance/transient_attributes_spec.rb
|
261
262
|
- spec/factory_girl/aliases_spec.rb
|
262
|
-
- spec/factory_girl/association_runner_spec.rb
|
263
263
|
- spec/factory_girl/attribute/association_spec.rb
|
264
264
|
- spec/factory_girl/attribute/dynamic_spec.rb
|
265
265
|
- spec/factory_girl/attribute/sequence_spec.rb
|
@@ -283,6 +283,7 @@ files:
|
|
283
283
|
- spec/factory_girl/strategy/build_spec.rb
|
284
284
|
- spec/factory_girl/strategy/create_spec.rb
|
285
285
|
- spec/factory_girl/strategy/stub_spec.rb
|
286
|
+
- spec/factory_girl/strategy_calculator_spec.rb
|
286
287
|
- spec/factory_girl/strategy_spec.rb
|
287
288
|
- spec/factory_girl_spec.rb
|
288
289
|
- spec/spec_helper.rb
|
@@ -364,7 +365,6 @@ test_files:
|
|
364
365
|
- spec/acceptance/traits_spec.rb
|
365
366
|
- spec/acceptance/transient_attributes_spec.rb
|
366
367
|
- spec/factory_girl/aliases_spec.rb
|
367
|
-
- spec/factory_girl/association_runner_spec.rb
|
368
368
|
- spec/factory_girl/attribute/association_spec.rb
|
369
369
|
- spec/factory_girl/attribute/dynamic_spec.rb
|
370
370
|
- spec/factory_girl/attribute/sequence_spec.rb
|
@@ -388,6 +388,7 @@ test_files:
|
|
388
388
|
- spec/factory_girl/strategy/build_spec.rb
|
389
389
|
- spec/factory_girl/strategy/create_spec.rb
|
390
390
|
- spec/factory_girl/strategy/stub_spec.rb
|
391
|
+
- spec/factory_girl/strategy_calculator_spec.rb
|
391
392
|
- spec/factory_girl/strategy_spec.rb
|
392
393
|
- spec/factory_girl_spec.rb
|
393
394
|
- spec/spec_helper.rb
|
@@ -1,50 +0,0 @@
|
|
1
|
-
module FactoryGirl
|
2
|
-
class AssociationRunner
|
3
|
-
def initialize(factory_name, strategy_name_or_object, overrides)
|
4
|
-
@factory_name = factory_name
|
5
|
-
@strategy_name_or_object = strategy_name_or_object
|
6
|
-
@overrides = overrides
|
7
|
-
end
|
8
|
-
|
9
|
-
def run(strategy_override = nil)
|
10
|
-
strategy_name_or_object = strategy_override || @strategy_name_or_object
|
11
|
-
strategy = StrategyCalculator.new(strategy_name_or_object).strategy
|
12
|
-
factory.run(strategy, @overrides)
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def factory
|
18
|
-
FactoryGirl.factory_by_name(@factory_name)
|
19
|
-
end
|
20
|
-
|
21
|
-
class StrategyCalculator
|
22
|
-
def initialize(name_or_object)
|
23
|
-
@name_or_object = name_or_object
|
24
|
-
end
|
25
|
-
|
26
|
-
def strategy
|
27
|
-
if strategy_is_object?
|
28
|
-
@name_or_object
|
29
|
-
else
|
30
|
-
strategy_name_to_object
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def strategy_is_object?
|
37
|
-
@name_or_object.is_a?(Class) && @name_or_object.ancestors.include?(::FactoryGirl::Strategy)
|
38
|
-
end
|
39
|
-
|
40
|
-
def strategy_name_to_object
|
41
|
-
case @name_or_object
|
42
|
-
when :build then Strategy::Build
|
43
|
-
when :create then Strategy::Create
|
44
|
-
when nil then Strategy::Create
|
45
|
-
else raise "unrecognized method #{@name_or_object}"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
@@ -1,31 +0,0 @@
|
|
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
|