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/registry_spec.rb
Christian Bruckmayer b86ec136b6
Raise KeyError instead of ArgumentError in Registry
to make use of did_you_mean gem.

The did_you_mean gem only supports NameError, NoMethodError
and KeyError. However, for NameError the message does also need
to match a certain format and we can not use a custom message
like 'Factory not registered ...'. Therefore using KeyError
is to only logical conclusion.

The did_you_mean gem makes use of the receiver attributes, but
in Ruby > 2.5 it is not possible to set the receiver and key attributes
on a KeyError (they are only set when the KeyError is raised in C).
We explored monkey patching KeyError for earlier versions of Ruby, but
it was a problematic solution.

Instead we can rescue the original KeyError, take the message from it,
which will already include the did_you_mean message, then customize
the message and re-raise a new KeyError with that customized message.
Starting in Ruby 2.6 this will not be necessary anymore
https://bugs.ruby-lang.org/issues/14313, so maybe we can get rid of it
for FactoryBot 6 or 7.

Fixes #992

Co-authored-by: Daniel Colson <danieljamescolson@gmail.com>
2018-12-16 22:25:04 -05:00

65 lines
2.2 KiB
Ruby

describe FactoryBot::Registry do
let(:registered_object) { double("registered object") }
let(:second_registered_object) { double("second registered object") }
subject { FactoryBot::Registry.new("Great thing") }
it { should be_kind_of(Enumerable) }
it "finds a registered object" do
subject.register(:object_name, registered_object)
expect(subject.find(:object_name)).to eq registered_object
end
it "finds a registered object with square brackets" do
subject.register(:object_name, registered_object)
expect(subject[:object_name]).to eq registered_object
end
it "raises when an object cannot be found" do
expect { subject.find(:object_name) }.
to raise_error(KeyError, "Great thing not registered: \"object_name\"")
end
it "includes a did_you_mean message" do
subject.register(:factory_bot, registered_object)
expect { subject.find(:factory_bit) }.
to raise_error(KeyError, /Did you mean\? "factory_bot"/)
end
it "adds and returns the object registered" do
expect(subject.register(:object_name, registered_object)).to eq registered_object
end
it "knows that an object is registered by symbol" do
subject.register(:object_name, registered_object)
expect(subject).to be_registered(:object_name)
end
it "knows that an object is registered by string" do
subject.register(:object_name, registered_object)
expect(subject).to be_registered("object_name")
end
it "knows when an object is not registered" do
expect(subject).not_to be_registered("bogus")
end
it "iterates registered objects" do
subject.register(:first_object, registered_object)
subject.register(:second_object, second_registered_object)
expect(subject.to_a).to eq [registered_object, second_registered_object]
end
it "does not include duplicate objects with registered under different names" do
subject.register(:first_object, registered_object)
subject.register(:second_object, registered_object)
expect(subject.to_a).to eq [registered_object]
end
it "clears registered factories" do
subject.register(:object_name, registered_object)
subject.clear
expect(subject.count).to be_zero
end
end