2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Registry do
|
2019-10-23 17:15:53 -04:00
|
|
|
it "is an enumerable" do
|
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
2011-01-25 17:55:40 -05:00
|
|
|
|
2019-10-23 17:15:53 -04:00
|
|
|
expect(registry).to be_kind_of(Enumerable)
|
|
|
|
end
|
2011-08-13 01:03:12 -04:00
|
|
|
|
2012-04-20 23:24:04 -04:00
|
|
|
it "finds a registered object" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
registered_object = double("registered object")
|
|
|
|
registry.register(:object_name, registered_object)
|
|
|
|
|
|
|
|
expect(registry.find(:object_name)).to eq registered_object
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
2012-04-23 16:33:12 -04:00
|
|
|
it "finds a registered object with square brackets" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
registered_object = double("registered object")
|
|
|
|
registry.register(:object_name, registered_object)
|
|
|
|
|
|
|
|
expect(registry[:object_name]).to eq registered_object
|
2012-04-23 16:33:12 -04:00
|
|
|
end
|
|
|
|
|
2012-04-22 17:44:03 -04:00
|
|
|
it "raises when an object cannot be found" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect { registry.find(:object_name) }
|
|
|
|
.to raise_error(KeyError, "Great thing not registered: \"object_name\"")
|
2018-12-14 08:27:48 -05:00
|
|
|
end
|
|
|
|
|
2020-07-10 11:52:25 -04:00
|
|
|
it "includes a did_you_mean message" do
|
|
|
|
registry = FactoryBot::Registry.new(:registry)
|
|
|
|
registered_object = double(:registered_object)
|
|
|
|
registry.register(:factory_bot, registered_object)
|
|
|
|
|
|
|
|
expect { registry.find(:factory_bit) }
|
|
|
|
.to raise_error(KeyError, /Did you mean\? "factory_bot"/)
|
|
|
|
end
|
|
|
|
|
2012-04-20 23:24:04 -04:00
|
|
|
it "adds and returns the object registered" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
registered_object = double("registered object")
|
|
|
|
|
|
|
|
expect(registry.register(:object_name, registered_object)).to eq registered_object
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
2012-04-20 23:24:04 -04:00
|
|
|
it "knows that an object is registered by symbol" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
registered_object = double("registered object")
|
|
|
|
registry.register(:object_name, registered_object)
|
|
|
|
|
|
|
|
expect(registry).to be_registered(:object_name)
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
2012-04-20 23:24:04 -04:00
|
|
|
it "knows that an object is registered by string" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
registered_object = double("registered object")
|
|
|
|
registry.register(:object_name, registered_object)
|
|
|
|
|
|
|
|
expect(registry).to be_registered("object_name")
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
2012-04-20 23:24:04 -04:00
|
|
|
it "knows when an object is not registered" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
|
|
|
|
expect(registry).not_to be_registered("bogus")
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
2012-04-20 23:24:04 -04:00
|
|
|
it "iterates registered objects" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
registered_object = double("registered object")
|
|
|
|
second_registered_object = double("second registered object")
|
|
|
|
registry.register(:first_object, registered_object)
|
|
|
|
registry.register(:second_object, second_registered_object)
|
|
|
|
|
|
|
|
expect(registry.to_a).to eq [registered_object, second_registered_object]
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
|
2012-04-20 23:24:04 -04:00
|
|
|
it "does not include duplicate objects with registered under different names" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
registered_object = double("registered object")
|
|
|
|
registry.register(:first_object, registered_object)
|
|
|
|
registry.register(:second_object, registered_object)
|
|
|
|
|
|
|
|
expect(registry.to_a).to eq [registered_object]
|
2011-06-30 18:26:06 -04:00
|
|
|
end
|
|
|
|
|
2011-05-19 10:56:45 -04:00
|
|
|
it "clears registered factories" do
|
2019-10-23 17:15:53 -04:00
|
|
|
registry = FactoryBot::Registry.new("Great thing")
|
|
|
|
registered_object = double("registered object")
|
|
|
|
registry.register(:object_name, registered_object)
|
|
|
|
registry.clear
|
|
|
|
|
|
|
|
expect(registry.count).to be_zero
|
2011-01-25 17:55:40 -05:00
|
|
|
end
|
|
|
|
end
|