2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "#add_attribute" do
|
2019-10-25 13:03:09 -04:00
|
|
|
it "declares a dynamic attribute on the factory when the proxy respects attributes" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
2012-04-20 16:59:39 -04:00
|
|
|
attribute_value = -> { "dynamic attribute" }
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.add_attribute(:attribute_name, &attribute_value)
|
2019-10-25 13:03:09 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(definition).to have_dynamic_declaration(:attribute_name)
|
|
|
|
.with_value(attribute_value)
|
2011-06-29 16:49:45 -04:00
|
|
|
end
|
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "declares a dynamic attribute on the factory when the proxy ignores attributes" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition, true)
|
2012-04-20 16:59:39 -04:00
|
|
|
attribute_value = -> { "dynamic attribute" }
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.add_attribute(:attribute_name, &attribute_value)
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(definition).to have_dynamic_declaration(:attribute_name)
|
|
|
|
.ignored
|
|
|
|
.with_value(attribute_value)
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "#transient" do
|
2011-10-14 15:14:43 -04:00
|
|
|
it "makes all attributes added ignored" do
|
2019-10-25 13:03:09 -04:00
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
2018-07-29 11:46:49 -04:00
|
|
|
attribute_value = -> { "dynamic_attribute" }
|
2014-04-28 17:39:44 -04:00
|
|
|
proxy.transient do
|
2018-07-29 11:46:49 -04:00
|
|
|
add_attribute(:attribute_name, &attribute_value)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2011-10-14 15:14:43 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(definition).to have_dynamic_declaration(:attribute_name)
|
|
|
|
.ignored
|
|
|
|
.with_value(attribute_value)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "#method_missing" do
|
2019-10-25 13:03:09 -04:00
|
|
|
it "declares an implicit declaration when called without args or a block" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
proxy.bogus
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
expect(definition).to have_implicit_declaration(:bogus).with_factory(definition)
|
2018-09-28 16:36:07 -04:00
|
|
|
end
|
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "declares an association when called with a ':factory' key" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
proxy.author factory: :user
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(definition).to have_association_declaration(:author)
|
|
|
|
.with_options(factory: :user)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "declares a dynamic attribute when called with a block" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
attribute_value = -> { "dynamic attribute" }
|
|
|
|
proxy.attribute_name(&attribute_value)
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(definition).to have_dynamic_declaration(:attribute_name)
|
|
|
|
.with_value(attribute_value)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "raises a NoMethodError when called with a static-attribute-like argument" do
|
|
|
|
definition = FactoryBot::Definition.new(:broken)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
invalid_call = -> { proxy.static_attributes_are_gone "true" }
|
2018-09-28 16:36:07 -04:00
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
expect(invalid_call).to raise_error(
|
|
|
|
NoMethodError,
|
|
|
|
"undefined method 'static_attributes_are_gone' in 'broken' factory\n" \
|
2020-06-05 15:15:18 -04:00
|
|
|
"Did you mean? 'static_attributes_are_gone { \"true\" }'\n"
|
2019-10-25 13:03:09 -04:00
|
|
|
)
|
2018-07-29 11:46:49 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "#sequence" do
|
2018-08-04 10:06:57 -04:00
|
|
|
def build_proxy(factory_name)
|
|
|
|
definition = FactoryBot::Definition.new(factory_name)
|
|
|
|
FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "creates a new sequence starting at 1" do
|
2019-10-25 13:03:09 -04:00
|
|
|
allow(FactoryBot::Sequence).to receive(:new).and_call_original
|
2018-08-04 10:06:57 -04:00
|
|
|
proxy = build_proxy(:factory)
|
2019-02-13 17:24:28 -05:00
|
|
|
|
2018-08-04 10:06:57 -04:00
|
|
|
proxy.sequence(:sequence)
|
|
|
|
|
2019-02-13 17:24:28 -05:00
|
|
|
expect(FactoryBot::Sequence).to have_received(:new).with(:sequence)
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2022-06-08 07:01:10 -04:00
|
|
|
it "creates a new sequence with an overridden starting value" do
|
2019-10-25 13:03:09 -04:00
|
|
|
allow(FactoryBot::Sequence).to receive(:new).and_call_original
|
2018-08-04 10:06:57 -04:00
|
|
|
proxy = build_proxy(:factory)
|
2019-02-13 17:24:28 -05:00
|
|
|
override = "override"
|
|
|
|
|
|
|
|
proxy.sequence(:sequence, override)
|
2018-08-04 10:06:57 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(FactoryBot::Sequence).to have_received(:new)
|
|
|
|
.with(:sequence, override)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "creates a new sequence with a block" do
|
2019-10-25 13:03:09 -04:00
|
|
|
allow(FactoryBot::Sequence).to receive(:new).and_call_original
|
2020-06-05 15:15:18 -04:00
|
|
|
sequence_block = proc { |n| "user+#{n}@example.com" }
|
2018-08-04 10:06:57 -04:00
|
|
|
proxy = build_proxy(:factory)
|
|
|
|
proxy.sequence(:sequence, 1, &sequence_block)
|
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(FactoryBot::Sequence).to have_received(:new)
|
|
|
|
.with(:sequence, 1, &sequence_block)
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "#association" do
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares an association" do
|
2018-10-28 19:12:34 -04:00
|
|
|
definition = FactoryBot::Definition.new(:definition_name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.association(:association_name)
|
2018-10-28 19:12:34 -04:00
|
|
|
|
|
|
|
expect(definition).to have_association_declaration(:association_name)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2010-07-06 21:15:16 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares an association with options" do
|
2018-10-28 19:12:34 -04:00
|
|
|
definition = FactoryBot::Definition.new(:definition_name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
|
2018-10-07 18:02:54 -04:00
|
|
|
proxy.association(:association_name, name: "Awesome")
|
2018-10-28 19:12:34 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(definition).to have_association_declaration(:association_name)
|
|
|
|
.with_options(name: "Awesome")
|
2010-07-06 21:15:16 -04:00
|
|
|
end
|
2018-10-28 19:12:34 -04:00
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "when passing a block raises an error" do
|
|
|
|
definition = FactoryBot::Definition.new(:post)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
2018-10-28 19:12:34 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect { proxy.association(:author) {} }
|
|
|
|
.to raise_error(
|
2019-10-25 13:03:09 -04:00
|
|
|
FactoryBot::AssociationDefinitionError,
|
2020-06-05 15:15:18 -04:00
|
|
|
"Unexpected block passed to 'author' association in 'post' factory"
|
2019-10-25 13:03:09 -04:00
|
|
|
)
|
2018-10-28 19:12:34 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
2010-07-06 21:15:16 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "adding callbacks" do
|
2019-10-25 13:03:09 -04:00
|
|
|
it "adding an :after_build callback succeeds" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
callback = -> { "my awesome callback!" }
|
|
|
|
|
|
|
|
proxy.after(:build, &callback)
|
2011-10-14 15:14:43 -04:00
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
expect(definition).to have_callback(:after_build).with_block(callback)
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "adding an :after_create callback succeeds" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
callback = -> { "my awesome callback!" }
|
|
|
|
|
|
|
|
proxy.after(:create, &callback)
|
|
|
|
|
|
|
|
expect(definition).to have_callback(:after_create).with_block(callback)
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "adding an :after_stub callback succeeds" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
callback = -> { "my awesome callback!" }
|
|
|
|
|
|
|
|
proxy.after(:stub, &callback)
|
|
|
|
expect(definition).to have_callback(:after_stub).with_block(callback)
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
2012-09-11 11:08:44 -04:00
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "adding both an :after_stub and an :after_create callback succeeds" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
callback = -> { "my awesome callback!" }
|
|
|
|
|
|
|
|
proxy.after(:stub, :create, &callback)
|
|
|
|
|
|
|
|
expect(definition).to have_callback(:after_stub).with_block(callback)
|
|
|
|
expect(definition).to have_callback(:after_create).with_block(callback)
|
2012-09-11 11:08:44 -04:00
|
|
|
end
|
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "adding both a :before_stub and a :before_create callback succeeds" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
callback = -> { "my awesome callback!" }
|
|
|
|
|
|
|
|
proxy.before(:stub, :create, &callback)
|
|
|
|
|
|
|
|
expect(definition).to have_callback(:before_stub).with_block(callback)
|
|
|
|
expect(definition).to have_callback(:before_create).with_block(callback)
|
2012-09-11 11:08:44 -04:00
|
|
|
end
|
|
|
|
|
2019-10-25 13:03:09 -04:00
|
|
|
it "adding both an :after_stub and a :before_create callback succeeds" do
|
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
|
|
|
callback = -> { "my awesome callback!" }
|
|
|
|
|
|
|
|
proxy.callback(:after_stub, :before_create, &callback)
|
|
|
|
|
|
|
|
expect(definition).to have_callback(:after_stub).with_block(callback)
|
|
|
|
expect(definition).to have_callback(:before_create).with_block(callback)
|
2012-09-11 11:08:44 -04:00
|
|
|
end
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "#to_create" do
|
2011-10-14 15:14:43 -04:00
|
|
|
it "accepts a block to run in place of #save!" do
|
2019-10-25 13:03:09 -04:00
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
2012-04-20 16:59:39 -04:00
|
|
|
to_create_block = ->(record) { record.persist }
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.to_create(&to_create_block)
|
2019-10-25 13:03:09 -04:00
|
|
|
|
|
|
|
expect(definition.to_create).to eq to_create_block
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "#factory" do
|
2011-10-14 15:14:43 -04:00
|
|
|
it "without options" do
|
2019-10-25 13:03:09 -04:00
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.factory(:child)
|
2019-10-25 13:03:09 -04:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(proxy.child_factories).to include([:child, {}, nil])
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "with options" do
|
2019-10-25 13:03:09 -04:00
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
2018-10-07 18:02:54 -04:00
|
|
|
proxy.factory(:child, awesome: true)
|
2019-10-25 13:03:09 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
expect(proxy.child_factories).to include([:child, {awesome: true}, nil])
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "with a block" do
|
2019-10-25 13:03:09 -04:00
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
2018-09-27 21:35:05 -04:00
|
|
|
child_block = -> {}
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.factory(:child, {}, &child_block)
|
2019-10-25 13:03:09 -04:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(proxy.child_factories).to include([:child, {}, child_block])
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "#trait" do
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares a trait" do
|
2019-10-25 13:03:09 -04:00
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
2020-06-05 15:15:18 -04:00
|
|
|
male_trait = proc { gender { "Male" } }
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.trait(:male, &male_trait)
|
2019-10-25 13:03:09 -04:00
|
|
|
|
|
|
|
expect(definition).to have_trait(:male).with_block(male_trait)
|
2010-11-12 16:21:16 -05:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2012-01-20 13:04:48 -05:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::DefinitionProxy, "#initialize_with" do
|
2012-01-20 13:04:48 -05:00
|
|
|
it "defines the constructor on the definition" do
|
2019-10-25 13:03:09 -04:00
|
|
|
definition = FactoryBot::Definition.new(:name)
|
|
|
|
proxy = FactoryBot::DefinitionProxy.new(definition)
|
2020-06-05 15:15:18 -04:00
|
|
|
constructor = proc { [] }
|
2012-01-20 13:04:48 -05:00
|
|
|
proxy.initialize_with(&constructor)
|
2019-10-25 13:03:09 -04:00
|
|
|
|
|
|
|
expect(definition.constructor).to eq constructor
|
2012-01-20 13:04:48 -05:00
|
|
|
end
|
|
|
|
end
|