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
2012-04-20 16:59:39 -04:00
FactoryGirl::Callback.new(:after_create, -> {}).name.should == :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
2012-04-20 16:59:39 -04:00
FactoryGirl::Callback.new("after_create", -> {}).name.should == :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)
2011-09-16 16:24:16 -04:00
ran_with.should == []
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)
2011-09-16 16:24:16 -04:00
ran_with.should == [:one]
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)
2011-09-16 16:24:16 -04:00
ran_with.should == [:one, :two]
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, -> {}) }.
2011-09-16 16:19:42 -04:00
to_not raise_error(FactoryGirl::InvalidCallbackNameError)
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