1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/factory_girl/callback_spec.rb

42 lines
1.3 KiB
Ruby
Raw Normal View History

2011-09-16 16:06:32 -04:00
require 'spec_helper'
describe FactoryGirl::Callback do
2011-09-16 16:19:42 -04:00
it "has a name" do
2013-01-18 13:27:57 -05:00
expect(FactoryGirl::Callback.new(:after_create, -> {}).name).to eq :after_create
2011-09-16 16:19:42 -04:00
end
2011-09-16 16:06:32 -04:00
2011-09-16 16:19:42 -04:00
it "converts strings to symbols" do
2013-01-18 13:27:57 -05:00
expect(FactoryGirl::Callback.new("after_create", -> {}).name).to eq :after_create
2011-09-16 16:19:42 -04:00
end
2011-09-16 16:06:32 -04:00
2011-09-16 16:24:16 -04:00
it "runs its block with no parameters" do
ran_with = nil
2012-04-20 16:59:39 -04:00
FactoryGirl::Callback.new(:after_create, -> { ran_with = [] }).run(:one, :two)
2013-01-18 13:27:57 -05:00
expect(ran_with).to eq []
2011-09-16 16:24:16 -04:00
end
it "runs its block with one parameter" do
ran_with = nil
2012-04-20 16:59:39 -04:00
FactoryGirl::Callback.new(:after_create, ->(one) { ran_with = [one] }).run(:one, :two)
2013-01-18 13:27:57 -05:00
expect(ran_with).to eq [:one]
2011-09-16 16:24:16 -04:00
end
it "runs its block with two parameters" do
ran_with = nil
2012-04-20 16:59:39 -04:00
FactoryGirl::Callback.new(:after_create, ->(one, two) { ran_with = [one, two] }).run(:one, :two)
2013-01-18 13:27:57 -05:00
expect(ran_with).to eq [:one, :two]
2011-09-16 16:24:16 -04:00
end
2011-09-16 16:19:42 -04:00
it "allows valid callback names to be assigned" do
FactoryGirl.callback_names.each do |callback_name|
2012-04-20 16:59:39 -04:00
expect { FactoryGirl::Callback.new(callback_name, -> {}) }.
2013-10-04 15:10:36 -04:00
to_not raise_error
2011-09-16 16:19:42 -04:00
end
end
2011-09-16 16:06:32 -04:00
2011-09-16 16:19:42 -04:00
it "raises if an invalid callback name is assigned" do
2012-04-20 16:59:39 -04:00
expect { FactoryGirl::Callback.new(:magic_fairies, -> {}) }.
2011-09-16 16:19:42 -04:00
to raise_error(FactoryGirl::InvalidCallbackNameError, /magic_fairies is not a valid callback name/)
end
2011-09-16 16:06:32 -04:00
end