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_bot/callback_spec.rb
Daniel Colson 7cfa233775 Remove code for registering callbacks
Before this commit, factory_bot registered default callbacks in a global
registry, and offered a `register_callback` method for registering any
custom callbacks (which would be fired within custom strategies).

The idea here seems to have been that we could check against this global
registry and then raise an error when referring to a callback that was
not registered.

However, nobody ever saw this error because we have always had the line:
`FactoryBot::Internal.register_callback(name)`, which registers
callbacks on the fly whenever they are referred to. (I noticed this when
a [change to default callbacks] accidentally duplicated `after_create`
instead of including `before_create`, and we didn't get any tests
failures.)

We have two options here:

1. Preserve the existing behavior by deleting all the callback
registration code. (That is what this commit does)

2. Change to the code try and capture what I assume was the original
intention. This would be a breaking change, so we would want to
introduce it in a major release. (See #1379)

I prefer preserving the existing behavior because we haven't seen any
issues around this, and making a breaking change for this doesn't seem
worthwhile.

[change to default callbacks]: f82e40c8c5 (diff-c6d30be672f880311a7df0820dc4fb21R12-R14)
2020-05-29 14:52:23 -04:00

27 lines
865 B
Ruby

describe FactoryBot::Callback do
it "has a name" do
expect(FactoryBot::Callback.new(:after_create, -> {}).name).to eq :after_create
end
it "converts strings to symbols" do
expect(FactoryBot::Callback.new("after_create", -> {}).name).to eq :after_create
end
it "runs its block with no parameters" do
ran_with = nil
FactoryBot::Callback.new(:after_create, -> { ran_with = [] }).run(:one, :two)
expect(ran_with).to eq []
end
it "runs its block with one parameter" do
ran_with = nil
FactoryBot::Callback.new(:after_create, ->(one) { ran_with = [one] }).run(:one, :two)
expect(ran_with).to eq [:one]
end
it "runs its block with two parameters" do
ran_with = nil
FactoryBot::Callback.new(:after_create, ->(one, two) { ran_with = [one, two] }).run(:one, :two)
expect(ran_with).to eq [:one, :two]
end
end