2010-06-10 14:58:47 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
describe FactoryGirl::DefinitionProxy, "#add_attribute" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
|
|
|
|
|
|
|
it "raises if both a block and value are given" do
|
2011-10-12 14:32:23 -04:00
|
|
|
expect {
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.add_attribute(:something, "great") { "will raise!" }
|
|
|
|
}.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given")
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares a static attribute on the factory" do
|
|
|
|
proxy.add_attribute(:attribute_name, "attribute value")
|
|
|
|
subject.should have_static_declaration(:attribute_name).with_value("attribute value")
|
|
|
|
end
|
2011-06-29 16:49:45 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares a dynamic attribute on the factory" do
|
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)
|
|
|
|
subject.should have_dynamic_declaration(:attribute_name).with_value(attribute_value)
|
2011-06-29 16:49:45 -04:00
|
|
|
end
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
2011-06-29 16:49:45 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
describe FactoryGirl::DefinitionProxy, "#add_attribute when the proxy ignores attributes" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject, true) }
|
2010-06-10 14:58:47 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "raises if both a block and value are given" do
|
|
|
|
expect {
|
|
|
|
proxy.add_attribute(:something, "great") { "will raise!" }
|
|
|
|
}.to raise_error(FactoryGirl::AttributeDefinitionError, "Both value and block given")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "declares a static attribute on the factory" do
|
|
|
|
proxy.add_attribute(:attribute_name, "attribute value")
|
|
|
|
subject.should have_static_declaration(:attribute_name).ignored.with_value("attribute value")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "declares a dynamic attribute on the factory" do
|
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)
|
|
|
|
subject.should have_dynamic_declaration(:attribute_name).ignored.with_value(attribute_value)
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
describe FactoryGirl::DefinitionProxy, "#ignore" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "makes all attributes added ignored" do
|
|
|
|
proxy.ignore do
|
|
|
|
add_attribute(:attribute_name, "attribute value")
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2011-10-14 15:14:43 -04:00
|
|
|
|
|
|
|
subject.should have_static_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
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
describe FactoryGirl::DefinitionProxy, "#method_missing" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares an implicit declaration without args or a block" do
|
|
|
|
proxy.bogus
|
|
|
|
subject.should have_implicit_declaration(:bogus).with_factory(subject)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares an association when :factory is passed" do
|
2012-03-09 17:20:38 -05:00
|
|
|
proxy.author factory: :user
|
|
|
|
subject.should have_association_declaration(:author).with_options(factory: :user)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares a static attribute" do
|
|
|
|
proxy.attribute_name "attribute value"
|
|
|
|
subject.should have_static_declaration(:attribute_name).with_value("attribute value")
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares a dynamic attribute" do
|
2012-04-20 16:59:39 -04:00
|
|
|
attribute_value = -> { "dynamic attribute" }
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.attribute_name &attribute_value
|
|
|
|
subject.should have_dynamic_declaration(:attribute_name).with_value(attribute_value)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
describe FactoryGirl::DefinitionProxy, "#sequence" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
before { FactoryGirl::Sequence.stubs(:new) }
|
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
|
|
|
|
proxy.sequence(:great)
|
2012-04-06 14:41:13 -04:00
|
|
|
FactoryGirl::Sequence.should have_received(:new).with(:great)
|
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 "creates a new sequence with an overridden starting vaue" do
|
|
|
|
proxy.sequence(:great, "C")
|
|
|
|
FactoryGirl::Sequence.should have_received(:new).with(:great, "C")
|
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
|
|
|
|
sequence_block = Proc.new {|n| "user+#{n}@example.com" }
|
|
|
|
proxy.sequence(:great, 1, &sequence_block)
|
|
|
|
FactoryGirl::Sequence.should have_received(:new).with(:great, 1, &sequence_block)
|
|
|
|
end
|
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
describe FactoryGirl::DefinitionProxy, "#association" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "declares an association" do
|
|
|
|
proxy.association(:association_name)
|
|
|
|
subject.should 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
|
2012-03-09 17:20:38 -05:00
|
|
|
proxy.association(:association_name, { name: "Awesome" })
|
|
|
|
subject.should have_association_declaration(:association_name).with_options(name: "Awesome")
|
2010-07-06 21:15:16 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
2010-07-06 21:15:16 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
describe FactoryGirl::DefinitionProxy, "adding callbacks" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
2012-04-20 16:59:39 -04:00
|
|
|
let(:callback) { -> { "my awesome callback!" } }
|
2011-10-14 15:14:43 -04:00
|
|
|
|
|
|
|
context "#after_build" do
|
|
|
|
before { proxy.after_build(&callback) }
|
2011-10-14 22:50:37 -04:00
|
|
|
it { should have_callback(:after_build).with_block(callback) }
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
context "#after_create" do
|
|
|
|
before { proxy.after_create(&callback) }
|
2011-10-14 22:50:37 -04:00
|
|
|
it { should have_callback(:after_create).with_block(callback) }
|
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
|
|
|
context "#after_stub" do
|
|
|
|
before { proxy.after_stub(&callback) }
|
2011-10-14 22:50:37 -04:00
|
|
|
it { should have_callback(:after_stub).with_block(callback) }
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
describe FactoryGirl::DefinitionProxy, "#to_create" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
|
|
|
|
|
|
|
it "accepts a block to run in place of #save!" do
|
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)
|
|
|
|
subject.to_create.should == 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
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
describe FactoryGirl::DefinitionProxy, "#factory" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "without options" do
|
|
|
|
proxy.factory(:child)
|
|
|
|
proxy.child_factories.should include([:child, {}, nil])
|
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
2011-10-14 15:14:43 -04:00
|
|
|
it "with options" do
|
2012-03-09 17:20:38 -05:00
|
|
|
proxy.factory(:child, { awesome: true })
|
|
|
|
proxy.child_factories.should include([:child, { awesome: true }, nil])
|
2011-10-14 15:14:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "with a block" do
|
2012-04-20 16:59:39 -04:00
|
|
|
child_block = -> { }
|
2011-10-14 15:14:43 -04:00
|
|
|
proxy.factory(:child, {}, &child_block)
|
|
|
|
proxy.child_factories.should include([:child, {}, child_block])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe FactoryGirl::DefinitionProxy, "#trait" do
|
2011-10-28 17:01:27 -04:00
|
|
|
subject { FactoryGirl::Definition.new }
|
2011-10-14 15:14:43 -04:00
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
|
|
|
|
|
|
|
it "declares a trait" do
|
|
|
|
male_trait = Proc.new { gender("Male") }
|
|
|
|
proxy.trait(:male, &male_trait)
|
|
|
|
subject.should 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
|
|
|
|
|
|
|
describe FactoryGirl::DefinitionProxy, "#initialize_with" do
|
|
|
|
subject { FactoryGirl::Definition.new }
|
|
|
|
let(:proxy) { FactoryGirl::DefinitionProxy.new(subject) }
|
|
|
|
|
|
|
|
it "defines the constructor on the definition" do
|
|
|
|
constructor = Proc.new { Array.new }
|
|
|
|
proxy.initialize_with(&constructor)
|
|
|
|
subject.constructor.should == constructor
|
|
|
|
end
|
|
|
|
end
|