2010-11-12 15:58:25 -05:00
|
|
|
describe "callbacks" do
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", first_name: :string, last_name: :string)
|
2010-11-12 15:58:25 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-03-09 17:20:38 -05:00
|
|
|
factory :user_with_callbacks, class: :user do
|
2012-05-04 16:48:46 -04:00
|
|
|
after(:stub) { |user| user.first_name = 'Stubby' }
|
|
|
|
after(:build) { |user| user.first_name = 'Buildy' }
|
|
|
|
after(:create) { |user| user.last_name = 'Createy' }
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
factory :user_with_inherited_callbacks, parent: :user_with_callbacks do
|
2012-05-04 16:48:46 -04:00
|
|
|
after(:stub) { |user| user.last_name = 'Double-Stubby' }
|
|
|
|
after(:build) { |user| user.first_name = 'Child-Buildy' }
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-04 16:48:46 -04:00
|
|
|
it "runs the after(:stub) callback when stubbing" do
|
2017-10-20 15:20:28 -04:00
|
|
|
user = FactoryBot.build_stubbed(:user_with_callbacks)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(user.first_name).to eq 'Stubby'
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
2012-05-04 16:48:46 -04:00
|
|
|
it "runs the after(:build) callback when building" do
|
2017-10-20 15:20:28 -04:00
|
|
|
user = FactoryBot.build(:user_with_callbacks)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(user.first_name).to eq 'Buildy'
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
2012-05-04 16:48:46 -04:00
|
|
|
it "runs both the after(:build) and after(:create) callbacks when creating" do
|
2017-10-20 15:20:28 -04:00
|
|
|
user = FactoryBot.create(:user_with_callbacks)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(user.first_name).to eq 'Buildy'
|
|
|
|
expect(user.last_name).to eq 'Createy'
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
|
|
|
|
2012-05-04 16:48:46 -04:00
|
|
|
it "runs both the after(:stub) callback on the factory and the inherited after(:stub) callback" do
|
2017-10-20 15:20:28 -04:00
|
|
|
user = FactoryBot.build_stubbed(:user_with_inherited_callbacks)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(user.first_name).to eq 'Stubby'
|
|
|
|
expect(user.last_name).to eq 'Double-Stubby'
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
2011-09-20 13:35:17 -04:00
|
|
|
|
|
|
|
it "runs child callback after parent callback" do
|
2017-10-20 15:20:28 -04:00
|
|
|
user = FactoryBot.build(:user_with_inherited_callbacks)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(user.first_name).to eq 'Child-Buildy'
|
2011-09-20 13:35:17 -04:00
|
|
|
end
|
2010-11-12 15:58:25 -05:00
|
|
|
end
|
2012-05-02 00:56:20 -04:00
|
|
|
|
2013-04-02 07:51:47 -04:00
|
|
|
describe 'callbacks using Symbol#to_proc' do
|
|
|
|
before do
|
|
|
|
define_model('User') do
|
|
|
|
def confirmed?
|
|
|
|
!!@confirmed
|
|
|
|
end
|
|
|
|
|
|
|
|
def confirm!
|
|
|
|
@confirmed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-04-02 07:51:47 -04:00
|
|
|
factory :user do
|
|
|
|
after :build, &:confirm!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'runs the callback correctly' do
|
2017-10-20 15:20:28 -04:00
|
|
|
user = FactoryBot.build(:user)
|
2013-04-02 07:51:47 -04:00
|
|
|
expect(user).to be_confirmed
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe "callbacks using syntax methods without referencing FactoryBot explicitly" do
|
2012-05-02 00:56:20 -04:00
|
|
|
before do
|
2015-04-15 21:50:33 -04:00
|
|
|
define_model("User", first_number: :integer, last_number: :integer)
|
2012-05-02 00:56:20 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-05-02 00:56:20 -04:00
|
|
|
sequence(:sequence_1)
|
|
|
|
sequence(:sequence_2)
|
|
|
|
sequence(:sequence_3)
|
|
|
|
|
|
|
|
factory :user do
|
2012-05-04 16:48:46 -04:00
|
|
|
after(:stub) { generate(:sequence_3) }
|
2015-04-15 21:50:33 -04:00
|
|
|
after(:build) { |user| user.first_number = generate(:sequence_1) }
|
|
|
|
after(:create) { |user, evaluator| user.last_number = generate(:sequence_2) }
|
2012-05-02 00:56:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works when the callback has no variables" do
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.build_stubbed(:user)
|
|
|
|
expect(FactoryBot.generate(:sequence_3)).to eq 2
|
2012-05-02 00:56:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works when the callback has one variable" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:user).first_number).to eq 1
|
2012-05-02 00:56:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works when the callback has two variables" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.create(:user).last_number).to eq 1
|
2012-05-02 00:56:20 -04:00
|
|
|
end
|
|
|
|
end
|
2012-05-04 16:48:46 -04:00
|
|
|
|
|
|
|
describe "custom callbacks" do
|
|
|
|
let(:custom_before) do
|
|
|
|
Class.new do
|
|
|
|
def result(evaluation)
|
|
|
|
evaluation.object.tap do |instance|
|
|
|
|
evaluation.notify(:before_custom, instance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:custom_after) do
|
|
|
|
Class.new do
|
|
|
|
def result(evaluation)
|
|
|
|
evaluation.object.tap do |instance|
|
|
|
|
evaluation.notify(:after_custom, instance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:totally_custom) do
|
|
|
|
Class.new do
|
|
|
|
def result(evaluation)
|
|
|
|
evaluation.object.tap do |instance|
|
|
|
|
evaluation.notify(:totally_custom, instance)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
define_model("User", first_name: :string, last_name: :string) do
|
|
|
|
def name
|
|
|
|
[first_name, last_name].join(" ")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.register_strategy(:custom_before, custom_before)
|
|
|
|
FactoryBot.register_strategy(:custom_after, custom_after)
|
|
|
|
FactoryBot.register_strategy(:totally_custom, totally_custom)
|
2012-05-04 16:48:46 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-05-04 16:48:46 -04:00
|
|
|
factory :user do
|
2018-07-29 11:30:02 -04:00
|
|
|
first_name { "John" }
|
|
|
|
last_name { "Doe" }
|
2012-05-04 16:48:46 -04:00
|
|
|
|
2013-12-14 22:33:15 -05:00
|
|
|
before(:custom) { |instance| instance.first_name = "Overridden First" }
|
|
|
|
after(:custom) { |instance| instance.last_name = "Overridden Last" }
|
2012-05-04 16:48:46 -04:00
|
|
|
callback(:totally_custom) do |instance|
|
|
|
|
instance.first_name = "Totally"
|
|
|
|
instance.last_name = "Custom"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "runs a custom before callback when the proper strategy executes" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:user).name).to eq "John Doe"
|
|
|
|
expect(FactoryBot.custom_before(:user).name).to eq "Overridden First Doe"
|
2012-05-04 16:48:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "runs a custom after callback when the proper strategy executes" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:user).name).to eq "John Doe"
|
|
|
|
expect(FactoryBot.custom_after(:user).name).to eq "John Overridden Last"
|
2012-05-04 16:48:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "runs a custom callback without prepending before or after when the proper strategy executes" do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:user).name).to eq "John Doe"
|
|
|
|
expect(FactoryBot.totally_custom(:user).name).to eq "Totally Custom"
|
2012-05-04 16:48:46 -04:00
|
|
|
end
|
|
|
|
end
|
2012-09-11 11:08:44 -04:00
|
|
|
|
|
|
|
describe 'binding a callback to multiple callbacks' do
|
|
|
|
before do
|
|
|
|
define_model('User', name: :string)
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2012-09-11 11:08:44 -04:00
|
|
|
factory :user do
|
|
|
|
callback(:before_create, :after_stub) do |instance|
|
|
|
|
instance.name = instance.name.upcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'binds the callback to creation' do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.create(:user, name: 'John Doe').name).to eq 'JOHN DOE'
|
2012-09-11 11:08:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not bind the callback to building' do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build(:user, name: 'John Doe').name).to eq 'John Doe'
|
2012-09-11 11:08:44 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'binds the callback to stubbing' do
|
2017-10-20 15:20:28 -04:00
|
|
|
expect(FactoryBot.build_stubbed(:user, name: 'John Doe').name).to eq 'JOHN DOE'
|
2012-09-11 11:08:44 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-08 11:00:22 -05:00
|
|
|
|
|
|
|
describe 'global callbacks' do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2013-02-08 11:00:22 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
define_model('User', name: :string)
|
|
|
|
define_model('Company', name: :string)
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2013-02-08 11:00:22 -05:00
|
|
|
after :build do |object|
|
|
|
|
object.name = case object.class.to_s
|
|
|
|
when 'User' then 'John Doe'
|
|
|
|
when 'Company' then 'Acme Suppliers'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
after :create do |object|
|
|
|
|
object.name = "#{object.name}!!!"
|
|
|
|
end
|
|
|
|
|
|
|
|
trait :awesome do
|
|
|
|
after :build do |object|
|
|
|
|
object.name = "___#{object.name}___"
|
|
|
|
end
|
|
|
|
|
|
|
|
after :create do |object|
|
|
|
|
object.name = "A#{object.name}Z"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :user do
|
|
|
|
after :build do |user|
|
|
|
|
user.name = user.name.downcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
factory :company do
|
|
|
|
after :build do |company|
|
|
|
|
company.name = company.name.upcase
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'triggers after build callbacks for all factories' do
|
|
|
|
expect(build(:user).name).to eq 'john doe'
|
|
|
|
expect(create(:user).name).to eq 'john doe!!!'
|
|
|
|
expect(create(:user, :awesome).name).to eq 'A___john doe___!!!Z'
|
|
|
|
expect(build(:company).name).to eq 'ACME SUPPLIERS'
|
|
|
|
end
|
|
|
|
end
|