2010-06-10 13:37:51 -04:00
|
|
|
require 'spec_helper'
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory do
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
|
|
|
@name = :user
|
2010-11-11 16:33:45 -05:00
|
|
|
@class = define_class('User')
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory = FactoryGirl::Factory.new(@name)
|
2011-10-07 15:00:38 -04:00
|
|
|
FactoryGirl.register_factory(@factory)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "has a factory name" do
|
2010-10-02 00:00:58 -04:00
|
|
|
@factory.name.should == @name
|
|
|
|
end
|
|
|
|
|
|
|
|
it "responds to factory_name" do
|
2010-06-10 14:58:47 -04:00
|
|
|
@factory.factory_name.should == @name
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "has a build class" do
|
2010-06-10 14:58:47 -04:00
|
|
|
@factory.build_class.should == @class
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "has a default strategy" do
|
2010-06-10 14:58:47 -04:00
|
|
|
@factory.default_strategy.should == :create
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-11-12 16:21:16 -05:00
|
|
|
it "passes a custom creation block" do
|
2011-08-12 22:06:10 -04:00
|
|
|
proxy = stub("proxy", :result => nil)
|
|
|
|
FactoryGirl::Proxy::Build.stubs(:new => proxy)
|
2010-11-12 16:21:16 -05:00
|
|
|
block = lambda {}
|
|
|
|
factory = FactoryGirl::Factory.new(:object)
|
|
|
|
factory.to_create(&block)
|
|
|
|
|
|
|
|
factory.run(FactoryGirl::Proxy::Build, {})
|
|
|
|
|
2011-08-12 22:06:10 -04:00
|
|
|
proxy.should have_received(:result).with(block)
|
2010-11-12 16:21:16 -05:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "returns associations" do
|
2010-06-24 09:45:57 -04:00
|
|
|
factory = FactoryGirl::Factory.new(:post)
|
2011-05-19 10:56:45 -04:00
|
|
|
FactoryGirl.register_factory(FactoryGirl::Factory.new(:admin))
|
2011-09-23 13:14:02 -04:00
|
|
|
factory.declare_attribute(FactoryGirl::Declaration::Association.new(:author, {}))
|
|
|
|
factory.declare_attribute(FactoryGirl::Declaration::Association.new(:editor, {}))
|
|
|
|
factory.declare_attribute(FactoryGirl::Declaration::Implicit.new(:admin, factory))
|
2010-06-10 14:58:47 -04:00
|
|
|
factory.associations.each do |association|
|
2011-05-19 10:56:45 -04:00
|
|
|
association.should be_association
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2011-05-19 10:56:45 -04:00
|
|
|
factory.associations.size.should == 3
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "raises for a self referencing association" do
|
2010-06-24 09:45:57 -04:00
|
|
|
factory = FactoryGirl::Factory.new(:post)
|
2010-06-10 14:58:47 -04:00
|
|
|
lambda {
|
2011-09-23 13:14:02 -04:00
|
|
|
factory.declare_attribute(FactoryGirl::Declaration::Association.new(:parent, { :factory => :post }))
|
2011-10-07 16:43:36 -04:00
|
|
|
factory.ensure_compiled
|
2010-06-24 09:45:57 -04:00
|
|
|
}.should raise_error(FactoryGirl::AssociationDefinitionError)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-09-15 16:56:20 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
describe "when overriding generated attributes with a hash" do
|
|
|
|
before do
|
|
|
|
@name = :name
|
|
|
|
@value = 'The price is right!'
|
|
|
|
@hash = { @name => @value }
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "returns the overridden value in the generated attributes" do
|
2011-09-23 13:14:02 -04:00
|
|
|
declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
|
|
|
|
@factory.declare_attribute(declaration)
|
2010-06-24 09:45:57 -04:00
|
|
|
result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
|
2010-06-10 14:58:47 -04:00
|
|
|
result[@name].should == @value
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "does not call a lazy attribute block for an overridden attribute" do
|
2011-10-15 11:18:27 -04:00
|
|
|
declaration = FactoryGirl::Declaration::Dynamic.new(@name, false, lambda { flunk })
|
2011-09-23 13:14:02 -04:00
|
|
|
@factory.declare_attribute(declaration)
|
2011-10-15 11:18:27 -04:00
|
|
|
@factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "overrides a symbol parameter with a string parameter" do
|
2011-09-23 13:14:02 -04:00
|
|
|
declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
|
|
|
|
@factory.declare_attribute(declaration)
|
2010-06-10 14:58:47 -04:00
|
|
|
@hash = { @name.to_s => @value }
|
2010-06-24 09:45:57 -04:00
|
|
|
result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
|
2010-06-10 14:58:47 -04:00
|
|
|
result[@name].should == @value
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
describe "overriding an attribute with an alias" do
|
|
|
|
before do
|
2011-09-23 13:14:02 -04:00
|
|
|
@factory.declare_attribute(FactoryGirl::Declaration::Static.new(:test, 'original'))
|
2010-06-10 14:58:47 -04:00
|
|
|
Factory.alias(/(.*)_alias/, '\1')
|
2010-06-24 09:45:57 -04:00
|
|
|
@result = @factory.run(FactoryGirl::Proxy::AttributesFor,
|
2010-06-10 14:58:47 -04:00
|
|
|
:test_alias => 'new')
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "uses the passed in value for the alias" do
|
2010-06-10 14:58:47 -04:00
|
|
|
@result[:test_alias].should == 'new'
|
2009-09-15 15:47:47 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "discards the predefined value for the attribute" do
|
2010-06-10 14:58:47 -04:00
|
|
|
@result[:test].should be_nil
|
2009-09-15 15:47:47 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-09-15 15:47:47 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "guesses the build class from the factory name" do
|
2010-06-10 14:58:47 -04:00
|
|
|
@factory.build_class.should == User
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "creates a new factory using the class of the parent" do
|
2011-10-07 15:00:38 -04:00
|
|
|
child = FactoryGirl::Factory.new(:child, :parent => @factory.name)
|
|
|
|
child.ensure_compiled
|
2010-06-10 14:58:47 -04:00
|
|
|
child.build_class.should == @factory.build_class
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "creates a new factory while overriding the parent class" do
|
2011-10-07 15:00:38 -04:00
|
|
|
child = FactoryGirl::Factory.new(:child, :class => String, :parent => @factory.name)
|
|
|
|
child.ensure_compiled
|
2010-06-10 14:58:47 -04:00
|
|
|
child.build_class.should == String
|
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory, "when defined with a custom class" do
|
2011-08-13 01:03:12 -04:00
|
|
|
subject { FactoryGirl::Factory.new(:author, :class => Float) }
|
|
|
|
its(:build_class) { should == Float }
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory, "when defined with a class instead of a name" do
|
2011-08-13 01:03:12 -04:00
|
|
|
let(:factory_class) { ArgumentError }
|
|
|
|
let(:name) { :argument_error }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
subject { FactoryGirl::Factory.new(factory_class) }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
its(:name) { should == name }
|
|
|
|
its(:build_class) { should == factory_class }
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory, "when defined with a custom class name" do
|
2011-08-13 01:03:12 -04:00
|
|
|
subject { FactoryGirl::Factory.new(:author, :class => :argument_error) }
|
|
|
|
its(:build_class) { should == ArgumentError }
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory, "with a name ending in s" do
|
2011-08-13 01:03:12 -04:00
|
|
|
let(:name) { :business }
|
|
|
|
let(:business_class) { Business }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
before { define_class('Business') }
|
|
|
|
subject { FactoryGirl::Factory.new(name) }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
its(:name) { should == name }
|
|
|
|
its(:build_class) { should == business_class }
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2010-03-17 18:49:35 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory, "with a string for a name" do
|
2011-08-13 01:03:12 -04:00
|
|
|
let(:name) { :string }
|
|
|
|
subject { FactoryGirl::Factory.new(name.to_s) }
|
|
|
|
its(:name) { should == name }
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2010-03-17 18:49:35 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory, "for namespaced class" do
|
2011-08-13 01:03:12 -04:00
|
|
|
let(:name) { :settings }
|
|
|
|
let(:settings_class) { Admin::Settings }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
before do
|
|
|
|
define_class("Admin")
|
|
|
|
define_class("Admin::Settings")
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
context "with a namespaced class with Namespace::Class syntax" do
|
|
|
|
subject { FactoryGirl::Factory.new(name, :class => "Admin::Settings") }
|
|
|
|
|
|
|
|
it "sets build_class correctly" do
|
|
|
|
subject.build_class.should == settings_class
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
context "with a namespaced class with namespace/class syntax" do
|
|
|
|
subject { FactoryGirl::Factory.new(name, :class => "admin/settings") }
|
|
|
|
|
|
|
|
it "sets build_class correctly" do
|
|
|
|
subject.build_class.should == settings_class
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory do
|
2011-08-19 17:05:53 -04:00
|
|
|
let(:factory_with_non_existent_strategy) do
|
2011-08-13 01:03:12 -04:00
|
|
|
FactoryGirl::Factory.new(:object, :default_strategy => :nonexistent) { }
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:factory_with_stub_strategy) do
|
|
|
|
FactoryGirl::Factory.new(:object, :default_strategy => :stub)
|
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
2011-08-13 01:03:12 -04:00
|
|
|
define_class("User")
|
|
|
|
define_class("Admin", User)
|
2011-10-07 15:00:38 -04:00
|
|
|
FactoryGirl.register_factory(factory_with_stub_strategy)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
it "raises an ArgumentError when trying to use a non-existent strategy" do
|
2011-08-19 17:05:53 -04:00
|
|
|
expect { factory_with_non_existent_strategy }.to raise_error(ArgumentError)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
it "creates a new factory with a specified default strategy" do
|
|
|
|
factory_with_stub_strategy.default_strategy.should == :stub
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
describe "defining a child factory without setting default strategy" do
|
2011-10-07 15:00:38 -04:00
|
|
|
subject { FactoryGirl::Factory.new(:other_object, :parent => factory_with_stub_strategy.name) }
|
|
|
|
before { subject.ensure_compiled }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
it "inherits default strategy from its parent" do
|
|
|
|
subject.default_strategy.should == :stub
|
2009-10-10 01:23:57 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
describe "defining a child factory with a default strategy" do
|
2011-10-07 15:00:38 -04:00
|
|
|
subject { FactoryGirl::Factory.new(:other_object, :default_strategy => :build, :parent => factory_with_stub_strategy.name) }
|
|
|
|
before { subject.ensure_compiled }
|
2010-01-08 00:07:41 -05:00
|
|
|
|
2011-08-13 01:03:12 -04:00
|
|
|
it "overrides the default strategy from parent" do
|
|
|
|
subject.default_strategy.should == :build
|
2010-01-08 00:07:41 -05:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
|
2011-06-30 18:27:25 -04:00
|
|
|
describe FactoryGirl::Factory, "human names" do
|
|
|
|
context "factory name without underscores" do
|
2011-08-13 01:03:12 -04:00
|
|
|
subject { FactoryGirl::Factory.new(:user) }
|
|
|
|
its(:names) { should == [:user] }
|
2011-06-30 18:27:25 -04:00
|
|
|
its(:human_names) { should == ["user"] }
|
|
|
|
end
|
2010-10-02 00:00:58 -04:00
|
|
|
|
2011-06-30 18:27:25 -04:00
|
|
|
context "factory name with underscores" do
|
2011-08-13 01:03:12 -04:00
|
|
|
subject { FactoryGirl::Factory.new(:happy_user) }
|
|
|
|
its(:names) { should == [:happy_user] }
|
2011-06-30 18:27:25 -04:00
|
|
|
its(:human_names) { should == ["happy user"] }
|
2010-10-02 00:00:58 -04:00
|
|
|
end
|
|
|
|
|
2011-06-30 18:27:25 -04:00
|
|
|
context "factory name with aliases" do
|
2011-08-13 01:03:12 -04:00
|
|
|
subject { FactoryGirl::Factory.new(:happy_user, :aliases => [:gleeful_user, :person]) }
|
|
|
|
its(:names) { should == [:happy_user, :gleeful_user, :person] }
|
2011-06-30 18:27:25 -04:00
|
|
|
its(:human_names) { should == ["happy user", "gleeful user", "person"] }
|
|
|
|
end
|
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
|
|
|
describe FactoryGirl::Factory, "running a factory" do
|
2011-09-23 13:14:02 -04:00
|
|
|
subject { FactoryGirl::Factory.new(:user) }
|
2011-10-07 18:19:27 -04:00
|
|
|
let(:attribute) { FactoryGirl::Attribute::Static.new(:name, "value", false) }
|
|
|
|
let(:declaration) { FactoryGirl::Declaration::Static.new(:name, "value", false) }
|
2011-09-23 13:14:02 -04:00
|
|
|
let(:proxy) { stub("proxy", :result => "result", :set => nil) }
|
|
|
|
let(:attributes) { [attribute] }
|
|
|
|
let(:attribute_list) { stub('attribute-list', :declarations => [declaration], :to_a => attributes) }
|
2011-08-20 01:09:29 -04:00
|
|
|
|
|
|
|
before do
|
2011-08-20 17:08:15 -04:00
|
|
|
define_model("User", :name => :string)
|
2011-09-23 13:14:02 -04:00
|
|
|
FactoryGirl::Declaration::Static.stubs(:new => declaration)
|
|
|
|
declaration.stubs(:to_attributes => attributes)
|
2011-09-16 16:06:32 -04:00
|
|
|
attribute.stubs(:add_to => nil)
|
2011-09-23 13:14:02 -04:00
|
|
|
FactoryGirl::Proxy::Build.stubs(:new => proxy)
|
|
|
|
subject.declare_attribute(declaration)
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "creates the right proxy using the build class when running" do
|
|
|
|
subject.run(FactoryGirl::Proxy::Build, {})
|
2011-10-20 14:19:09 -04:00
|
|
|
FactoryGirl::Proxy::Build.should have_received(:new).with(subject.build_class, [])
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the attribute to the proxy when running" do
|
|
|
|
subject.run(FactoryGirl::Proxy::Build, {})
|
|
|
|
attribute.should have_received(:add_to).with(proxy)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the result from the proxy when running" do
|
|
|
|
subject.run(FactoryGirl::Proxy::Build, {}).should == "result"
|
|
|
|
proxy.should have_received(:result).with(nil)
|
|
|
|
end
|
2011-08-20 17:08:15 -04:00
|
|
|
|
|
|
|
it "sets overrides once on the factory" do
|
|
|
|
subject.run(FactoryGirl::Proxy::Build, { :name => "John Doe" })
|
|
|
|
proxy.should have_received(:set).once
|
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|