factory_girl 4.0.0 → 4.1.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/GETTING_STARTED.md +11 -1
- data/Gemfile.lock +1 -1
- data/NEWS +4 -0
- 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/definition_proxy.rb +9 -7
- data/lib/factory_girl/version.rb +1 -1
- data/spec/acceptance/callbacks_spec.rb +26 -0
- data/spec/factory_girl/definition_proxy_spec.rb +18 -0
- metadata +2 -2
data/GETTING_STARTED.md
CHANGED
@@ -101,7 +101,7 @@ are defined in files at the following locations:
|
|
101
101
|
Using factories
|
102
102
|
---------------
|
103
103
|
|
104
|
-
factory\_girl supports several different build strategies: build, create, attributes\_for and
|
104
|
+
factory\_girl supports several different build strategies: build, create, attributes\_for and build\_stubbed:
|
105
105
|
|
106
106
|
```ruby
|
107
107
|
# Returns a User instance that's not saved
|
@@ -714,6 +714,16 @@ Calling FactoryGirl.create will invoke both `after_build` and `after_create` cal
|
|
714
714
|
|
715
715
|
Also, like standard attributes, child factories will inherit (and can also define) callbacks from their parent factory.
|
716
716
|
|
717
|
+
Multiple callbacks can be assigned to run a block; this is useful when building various strategies that run the same code (since there are no callbacks that are shared across all strategies).
|
718
|
+
|
719
|
+
```ruby
|
720
|
+
factory :user do
|
721
|
+
callback(:after_stub, :before_create) { do_something }
|
722
|
+
after(:stub, :create) { do_something_else }
|
723
|
+
before(:create, :custom) { do_a_third_thing }
|
724
|
+
end
|
725
|
+
```
|
726
|
+
|
717
727
|
Modifying factories
|
718
728
|
-------------------
|
719
729
|
|
data/Gemfile.lock
CHANGED
data/NEWS
CHANGED
data/gemfiles/3.0.gemfile.lock
CHANGED
data/gemfiles/3.1.gemfile.lock
CHANGED
data/gemfiles/3.2.gemfile.lock
CHANGED
@@ -158,17 +158,19 @@ module FactoryGirl
|
|
158
158
|
@definition.define_constructor(&block)
|
159
159
|
end
|
160
160
|
|
161
|
-
def before(
|
162
|
-
callback("before_#{name}", &block)
|
161
|
+
def before(*names, &block)
|
162
|
+
callback(*names.map {|name| "before_#{name}" }, &block)
|
163
163
|
end
|
164
164
|
|
165
|
-
def after(
|
166
|
-
callback("after_#{name}", &block)
|
165
|
+
def after(*names, &block)
|
166
|
+
callback(*names.map {|name| "after_#{name}" }, &block)
|
167
167
|
end
|
168
168
|
|
169
|
-
def callback(
|
170
|
-
|
171
|
-
|
169
|
+
def callback(*names, &block)
|
170
|
+
names.each do |name|
|
171
|
+
FactoryGirl.register_callback(name)
|
172
|
+
@definition.add_callback(Callback.new(name, block))
|
173
|
+
end
|
172
174
|
end
|
173
175
|
end
|
174
176
|
end
|
data/lib/factory_girl/version.rb
CHANGED
@@ -149,3 +149,29 @@ describe "custom callbacks" do
|
|
149
149
|
FactoryGirl.totally_custom(:user).name.should == "Totally Custom"
|
150
150
|
end
|
151
151
|
end
|
152
|
+
|
153
|
+
describe 'binding a callback to multiple callbacks' do
|
154
|
+
before do
|
155
|
+
define_model('User', name: :string)
|
156
|
+
|
157
|
+
FactoryGirl.define do
|
158
|
+
factory :user do
|
159
|
+
callback(:before_create, :after_stub) do |instance|
|
160
|
+
instance.name = instance.name.upcase
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'binds the callback to creation' do
|
167
|
+
FactoryGirl.create(:user, name: 'John Doe').name.should == 'JOHN DOE'
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'does not bind the callback to building' do
|
171
|
+
FactoryGirl.build(:user, name: 'John Doe').name.should == 'John Doe'
|
172
|
+
end
|
173
|
+
|
174
|
+
it 'binds the callback to stubbing' do
|
175
|
+
FactoryGirl.build_stubbed(:user, name: 'John Doe').name.should == 'JOHN DOE'
|
176
|
+
end
|
177
|
+
end
|
@@ -140,6 +140,24 @@ describe FactoryGirl::DefinitionProxy, "adding callbacks" do
|
|
140
140
|
before { proxy.after(:stub, &callback) }
|
141
141
|
it { should have_callback(:after_stub).with_block(callback) }
|
142
142
|
end
|
143
|
+
|
144
|
+
context "#after(:stub, :create)" do
|
145
|
+
before { proxy.after(:stub, :create, &callback) }
|
146
|
+
it { should have_callback(:after_stub).with_block(callback) }
|
147
|
+
it { should have_callback(:after_create).with_block(callback) }
|
148
|
+
end
|
149
|
+
|
150
|
+
context "#before(:stub, :create)" do
|
151
|
+
before { proxy.before(:stub, :create, &callback) }
|
152
|
+
it { should have_callback(:before_stub).with_block(callback) }
|
153
|
+
it { should have_callback(:before_create).with_block(callback) }
|
154
|
+
end
|
155
|
+
|
156
|
+
context "#callback(:after_stub, :before_create)" do
|
157
|
+
before { proxy.callback(:after_stub, :before_create, &callback) }
|
158
|
+
it { should have_callback(:after_stub).with_block(callback) }
|
159
|
+
it { should have_callback(:before_create).with_block(callback) }
|
160
|
+
end
|
143
161
|
end
|
144
162
|
|
145
163
|
describe FactoryGirl::DefinitionProxy, "#to_create" do
|
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: 4.
|
4
|
+
version: 4.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-
|
13
|
+
date: 2012-09-11 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|