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, "registering a factory" do
|
2010-06-10 13:37:51 -04:00
|
|
|
before do
|
2010-06-10 14:58:47 -04:00
|
|
|
@name = :user
|
|
|
|
@factory = "factory"
|
2010-11-11 17:34:01 -05:00
|
|
|
@aliases = [:one, :two]
|
2010-10-02 00:00:58 -04:00
|
|
|
stub(@factory).name { @name }
|
2010-11-11 17:34:01 -05:00
|
|
|
stub(@factory).aliases { @aliases }
|
2010-06-10 13:37:51 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should add the factory to the list of factories" do
|
2010-06-24 09:45:57 -04:00
|
|
|
FactoryGirl.register_factory(@factory)
|
|
|
|
FactoryGirl.factory_by_name(@name).should == @factory
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-09-15 15:47:47 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should not allow a duplicate factory definition" do
|
|
|
|
lambda {
|
2010-06-24 09:45:57 -04:00
|
|
|
2.times { FactoryGirl.register_factory(@factory) }
|
|
|
|
}.should raise_error(FactoryGirl::DuplicateDefinitionError)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2010-11-11 17:34:01 -05:00
|
|
|
|
|
|
|
it "registers aliases" do
|
|
|
|
FactoryGirl.register_factory(@factory)
|
|
|
|
@aliases.each do |name|
|
|
|
|
FactoryGirl.factory_by_name(name).should == @factory
|
|
|
|
end
|
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-10-30 13:25:47 -04:00
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory do
|
2010-06-10 14:58:47 -04:00
|
|
|
include DefinesConstants
|
2009-10-30 13:25:47 -04:00
|
|
|
|
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)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should have 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
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should have a build class" do
|
|
|
|
@factory.build_class.should == @class
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should have a default strategy" do
|
|
|
|
@factory.default_strategy.should == :create
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should not allow the same attribute to be added twice" do
|
|
|
|
lambda {
|
2010-06-24 09:45:57 -04:00
|
|
|
2.times { @factory.define_attribute FactoryGirl::Attribute::Static.new(:name, 'value') }
|
|
|
|
}.should raise_error(FactoryGirl::AttributeDefinitionError)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-06-26 06:42:31 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should add a callback attribute when defining a callback" do
|
2010-06-24 09:45:57 -04:00
|
|
|
mock(FactoryGirl::Attribute::Callback).new(:after_create, is_a(Proc)) { 'after_create callback' }
|
2010-06-10 14:58:47 -04:00
|
|
|
@factory.add_callback(:after_create) {}
|
|
|
|
@factory.attributes.should include('after_create callback')
|
|
|
|
end
|
2009-06-26 06:42:31 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should raise an InvalidCallbackNameError when defining a callback with an invalid name" do
|
|
|
|
lambda{
|
|
|
|
@factory.add_callback(:invalid_callback_name) {}
|
2010-06-24 09:45:57 -04:00
|
|
|
}.should raise_error(FactoryGirl::InvalidCallbackNameError)
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-06-30 06:32:55 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
describe "after adding an attribute" do
|
|
|
|
before do
|
|
|
|
@attribute = "attribute"
|
|
|
|
@proxy = "proxy"
|
2009-06-30 06:32:55 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
stub(@attribute).name { :name }
|
|
|
|
stub(@attribute).add_to
|
|
|
|
stub(@proxy).set
|
|
|
|
stub(@proxy).result { 'result' }
|
2010-06-24 09:45:57 -04:00
|
|
|
stub(FactoryGirl::Attribute::Static).new { @attribute }
|
|
|
|
stub(FactoryGirl::Proxy::Build).new { @proxy }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
@factory.define_attribute(@attribute)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should create the right proxy using the build class when running" do
|
2010-06-24 09:45:57 -04:00
|
|
|
mock(FactoryGirl::Proxy::Build).new(@factory.build_class) { @proxy }
|
|
|
|
@factory.run(FactoryGirl::Proxy::Build, {})
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should add the attribute to the proxy when running" do
|
|
|
|
mock(@attribute).add_to(@proxy)
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory.run(FactoryGirl::Proxy::Build, {})
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should return the result from the proxy when running" do
|
|
|
|
mock(@proxy).result() { 'result' }
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory.run(FactoryGirl::Proxy::Build, {}).should == 'result'
|
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
|
|
|
it "should return associations" do
|
2010-06-24 09:45:57 -04:00
|
|
|
factory = FactoryGirl::Factory.new(:post)
|
|
|
|
factory.define_attribute(FactoryGirl::Attribute::Association.new(:author, :author, {}))
|
|
|
|
factory.define_attribute(FactoryGirl::Attribute::Association.new(:editor, :editor, {}))
|
2010-06-10 14:58:47 -04:00
|
|
|
factory.associations.each do |association|
|
2010-06-24 09:45:57 -04:00
|
|
|
association.should be_a(FactoryGirl::Attribute::Association)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
factory.associations.size.should == 2
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should raise 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 {
|
2010-06-24 09:45:57 -04:00
|
|
|
factory.define_attribute(FactoryGirl::Attribute::Association.new(:parent, :post, {}))
|
|
|
|
}.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
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should return the overridden value in the generated attributes" do
|
2010-06-24 09:45:57 -04:00
|
|
|
attr = FactoryGirl::Attribute::Static.new(@name, 'The price is wrong, Bob!')
|
2010-06-10 14:58:47 -04:00
|
|
|
@factory.define_attribute(attr)
|
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
|
|
|
it "should not call a lazy attribute block for an overridden attribute" do
|
2010-06-24 09:45:57 -04:00
|
|
|
attr = FactoryGirl::Attribute::Dynamic.new(@name, lambda { flunk })
|
2010-06-10 14:58:47 -04:00
|
|
|
@factory.define_attribute(attr)
|
2010-06-24 09:45:57 -04:00
|
|
|
result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should override a symbol parameter with a string parameter" do
|
2010-06-24 09:45:57 -04:00
|
|
|
attr = FactoryGirl::Attribute::Static.new(@name, 'The price is wrong, Bob!')
|
2010-06-10 14:58:47 -04:00
|
|
|
@factory.define_attribute(attr)
|
|
|
|
@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
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory.define_attribute(FactoryGirl::Attribute::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
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should use the passed in value for the alias" do
|
|
|
|
@result[:test_alias].should == 'new'
|
2009-09-15 15:47:47 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should discard the predefined value for the attribute" do
|
|
|
|
@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
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should guess the build class from the factory name" do
|
|
|
|
@factory.build_class.should == User
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should create a new factory using the class of the parent" do
|
2010-06-24 09:45:57 -04:00
|
|
|
child = FactoryGirl::Factory.new(:child)
|
2010-06-10 14:58:47 -04:00
|
|
|
child.inherit_from(@factory)
|
|
|
|
child.build_class.should == @factory.build_class
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should create a new factory while overriding the parent class" do
|
2010-06-24 09:45:57 -04:00
|
|
|
child = FactoryGirl::Factory.new(:child, :class => String)
|
2010-06-10 14:58:47 -04:00
|
|
|
child.inherit_from(@factory)
|
|
|
|
child.build_class.should == String
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
describe "given a parent with attributes" do
|
|
|
|
before do
|
|
|
|
@parent_attr = :name
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory.define_attribute(FactoryGirl::Attribute::Static.new(@parent_attr, 'value'))
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should create a new factory with attributes of the parent" do
|
2010-06-24 09:45:57 -04:00
|
|
|
child = FactoryGirl::Factory.new(:child)
|
2010-06-10 14:58:47 -04:00
|
|
|
child.inherit_from(@factory)
|
|
|
|
child.attributes.size.should == 1
|
|
|
|
child.attributes.first.name.should == @parent_attr
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should allow a child to define additional attributes" do
|
2010-06-24 09:45:57 -04:00
|
|
|
child = FactoryGirl::Factory.new(:child)
|
|
|
|
child.define_attribute(FactoryGirl::Attribute::Static.new(:email, 'value'))
|
2010-06-10 14:58:47 -04:00
|
|
|
child.inherit_from(@factory)
|
|
|
|
child.attributes.size.should == 2
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should allow to override parent attributes" do
|
2010-06-24 09:45:57 -04:00
|
|
|
child = FactoryGirl::Factory.new(:child)
|
|
|
|
@child_attr = FactoryGirl::Attribute::Static.new(@parent_attr, 'value')
|
2010-06-10 14:58:47 -04:00
|
|
|
child.define_attribute(@child_attr)
|
|
|
|
child.inherit_from(@factory)
|
|
|
|
child.attributes.size.should == 1
|
|
|
|
child.attributes.first.should == @child_attr
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2010-09-03 12:07:37 -04:00
|
|
|
|
|
|
|
it "should allow to use parent attributes in defining additional attributes" do
|
|
|
|
User.class_eval { attr_accessor :name, :email }
|
|
|
|
|
|
|
|
child = FactoryGirl::Factory.new(:child)
|
|
|
|
@child_attr = FactoryGirl::Attribute::Dynamic.new(:email, lambda {|u| "#{u.name}@example.com"})
|
|
|
|
child.define_attribute(@child_attr)
|
|
|
|
child.inherit_from(@factory)
|
|
|
|
child.attributes.size.should == 2
|
|
|
|
|
|
|
|
result = child.run(FactoryGirl::Proxy::Build, {})
|
|
|
|
result.email.should == 'value@example.com'
|
|
|
|
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
|
|
|
it "inherit all callbacks" do
|
|
|
|
@factory.add_callback(:after_stub) { |object| object.name = 'Stubby' }
|
2010-06-24 09:45:57 -04:00
|
|
|
child = FactoryGirl::Factory.new(:child)
|
2010-06-10 14:58:47 -04:00
|
|
|
child.inherit_from(@factory)
|
2010-06-24 09:45:57 -04:00
|
|
|
child.attributes.last.should be_kind_of(FactoryGirl::Attribute::Callback)
|
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, "when defined with a custom class" do
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
|
|
|
@class = Float
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory = FactoryGirl::Factory.new(:author, :class => @class)
|
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
|
|
|
it "should use the specified class as the build class" do
|
|
|
|
@factory.build_class.should == @class
|
|
|
|
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 class instead of a name" do
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
|
|
|
@class = ArgumentError
|
|
|
|
@name = :argument_error
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory = FactoryGirl::Factory.new(@class)
|
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
|
|
|
it "should guess the name from the class" do
|
2010-10-02 00:00:58 -04:00
|
|
|
@factory.name.should == @name
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should use the class as the build class" do
|
|
|
|
@factory.build_class.should == @class
|
|
|
|
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 name" do
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
|
|
|
@class = ArgumentError
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory = FactoryGirl::Factory.new(:author, :class => :argument_error)
|
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
|
|
|
it "should use the specified class as the build class" do
|
|
|
|
@factory.build_class.should == @class
|
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-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory, "with a name ending in s" do
|
2010-06-10 14:58:47 -04:00
|
|
|
include DefinesConstants
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
2010-11-11 16:33:45 -05:00
|
|
|
define_class('Business')
|
2010-06-10 14:58:47 -04:00
|
|
|
@name = :business
|
|
|
|
@class = Business
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory = FactoryGirl::Factory.new(@name)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should have a factory name" do
|
2010-10-02 00:00:58 -04:00
|
|
|
@factory.name.should == @name
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should have a build class" do
|
|
|
|
@factory.build_class.should == @class
|
|
|
|
end
|
|
|
|
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
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
|
|
|
@name = :string
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory = FactoryGirl::Factory.new(@name.to_s) {}
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2010-03-17 18:49:35 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should convert the string to a symbol" do
|
2010-10-02 00:00:58 -04:00
|
|
|
@factory.name.should == @name
|
2010-03-17 18:49:35 -04:00
|
|
|
end
|
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, "registered with a string name" do
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
|
|
|
@name = :string
|
2010-06-24 09:45:57 -04:00
|
|
|
@factory = FactoryGirl::Factory.new(@name)
|
|
|
|
FactoryGirl.register_factory(@factory)
|
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
|
|
|
it "should store the factory using a symbol" do
|
2010-06-24 09:45:57 -04:00
|
|
|
FactoryGirl.factories[@name].should == @factory
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-09-07 19:20:02 -04:00
|
|
|
describe FactoryGirl::Factory, "registered with a custom name" do
|
|
|
|
before do
|
|
|
|
@actual_name = :string
|
|
|
|
@custom_name = :words
|
|
|
|
@factory = FactoryGirl::Factory.new(@actual_name)
|
|
|
|
|
|
|
|
FactoryGirl.register_factory(@factory, :as => @custom_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "finds the factory using the custom name" do
|
|
|
|
FactoryGirl.factory_by_name(@custom_name).should == @factory
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-24 09:45:57 -04:00
|
|
|
describe FactoryGirl::Factory, "for namespaced class" do
|
2010-06-10 14:58:47 -04:00
|
|
|
include DefinesConstants
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
2010-11-11 16:33:45 -05:00
|
|
|
define_class('Admin')
|
|
|
|
define_class('Admin::Settings')
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
@name = :settings
|
|
|
|
@class = Admin::Settings
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should build namespaced class passed by string" do
|
2010-06-24 09:45:57 -04:00
|
|
|
factory = FactoryGirl::Factory.new(@name.to_s, :class => @class.name)
|
2010-06-10 14:58:47 -04:00
|
|
|
factory.build_class.should == @class
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should build Admin::Settings class from Admin::Settings string" do
|
2010-06-24 09:45:57 -04:00
|
|
|
factory = FactoryGirl::Factory.new(@name.to_s, :class => 'admin/settings')
|
2010-06-10 14:58:47 -04:00
|
|
|
factory.build_class.should == @class
|
|
|
|
end
|
|
|
|
end
|
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
|
|
|
include DefinesConstants
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
before do
|
2010-11-11 16:33:45 -05:00
|
|
|
define_class('User')
|
|
|
|
define_class('Admin', User)
|
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
|
|
|
it "should raise an ArgumentError when trying to use a non-existent strategy" do
|
|
|
|
lambda {
|
2010-06-24 09:45:57 -04:00
|
|
|
FactoryGirl::Factory.new(:object, :default_strategy => :nonexistent) {}
|
2010-06-10 14:58:47 -04:00
|
|
|
}.should raise_error(ArgumentError)
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should create a new factory with a specified default strategy" do
|
2010-06-24 09:45:57 -04:00
|
|
|
factory = FactoryGirl::Factory.new(:object, :default_strategy => :stub)
|
2010-06-10 14:58:47 -04:00
|
|
|
factory.default_strategy.should == :stub
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
describe 'defining a child factory without setting default strategy' do
|
2009-04-11 11:27:23 -04:00
|
|
|
before do
|
2010-06-24 09:45:57 -04:00
|
|
|
@parent = FactoryGirl::Factory.new(:object, :default_strategy => :stub)
|
|
|
|
@child = FactoryGirl::Factory.new(:child_object)
|
2010-06-10 14:58:47 -04:00
|
|
|
@child.inherit_from(@parent)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should inherit default strategy from its parent" do
|
|
|
|
@child.default_strategy.should == :stub
|
2009-10-10 01:23:57 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
describe 'defining a child factory with a default strategy' do
|
|
|
|
before do
|
2010-06-24 09:45:57 -04:00
|
|
|
@parent = FactoryGirl::Factory.new(:object, :default_strategy => :stub)
|
|
|
|
@child = FactoryGirl::Factory.new(:child_object2, :default_strategy => :build)
|
2010-06-10 14:58:47 -04:00
|
|
|
@child.inherit_from(@parent)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2010-01-08 00:07:41 -05:00
|
|
|
|
2010-06-10 14:58:47 -04:00
|
|
|
it "should override the default strategy from parent" do
|
|
|
|
@child.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
|
|
|
|
|
2010-10-02 00:00:58 -04:00
|
|
|
describe FactoryGirl::Factory, "with an underscore in the name" do
|
|
|
|
subject { FactoryGirl::Factory.new("happy_users") }
|
|
|
|
|
|
|
|
it "has a human name" do
|
|
|
|
subject.human_name.should == 'happy users'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-11-11 17:34:01 -05:00
|
|
|
|
|
|
|
describe FactoryGirl::Factory, "with aliases" do
|
|
|
|
it "registers the aliases" do
|
|
|
|
aliased_name = :guest
|
|
|
|
factory = FactoryGirl::Factory.new("user", :aliases => [aliased_name])
|
|
|
|
factory.aliases.should == [aliased_name]
|
|
|
|
end
|
|
|
|
end
|