1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/factory_girl/factory_spec.rb

412 lines
12 KiB
Ruby
Raw Normal View History

require 'spec_helper'
2009-04-11 11:27:23 -04:00
describe FactoryGirl::Factory, "registering a factory" do
before do
@name = :user
@factory = "factory"
@aliases = [:one, :two]
stub(@factory).name { @name }
stub(@factory).aliases { @aliases }
end
it "should add the factory to the list of factories" do
FactoryGirl.register_factory(@factory)
FactoryGirl.factory_by_name(@name).should == @factory
end
2009-09-15 15:47:47 -04:00
it "should not allow a duplicate factory definition" do
lambda {
2.times { FactoryGirl.register_factory(@factory) }
}.should raise_error(FactoryGirl::DuplicateDefinitionError)
end
it "registers aliases" do
FactoryGirl.register_factory(@factory)
@aliases.each do |name|
FactoryGirl.factory_by_name(name).should == @factory
end
end
end
describe FactoryGirl::Factory do
include DefinesConstants
before do
@name = :user
2010-11-11 16:33:45 -05:00
@class = define_class('User')
@factory = FactoryGirl::Factory.new(@name)
2009-04-11 11:27:23 -04:00
end
it "should have a factory name" do
@factory.name.should == @name
end
it "responds to factory_name" do
@factory.factory_name.should == @name
end
2009-04-11 11:27:23 -04:00
it "should have a build class" do
@factory.build_class.should == @class
end
2009-04-11 11:27:23 -04:00
it "should have a default strategy" do
@factory.default_strategy.should == :create
end
2009-04-11 11:27:23 -04:00
it "should not allow the same attribute to be added twice" do
lambda {
2.times { @factory.define_attribute FactoryGirl::Attribute::Static.new(:name, 'value') }
}.should raise_error(FactoryGirl::AttributeDefinitionError)
end
it "should add a callback attribute when defining a callback" do
mock(FactoryGirl::Attribute::Callback).new(:after_create, is_a(Proc)) { 'after_create callback' }
@factory.add_callback(:after_create) {}
@factory.attributes.should include('after_create callback')
end
it "should raise an InvalidCallbackNameError when defining a callback with an invalid name" do
lambda{
@factory.add_callback(:invalid_callback_name) {}
}.should raise_error(FactoryGirl::InvalidCallbackNameError)
end
2009-06-30 06:32:55 -04:00
describe "after adding an attribute" do
before do
@attribute = "attribute"
@proxy = "proxy"
2009-06-30 06:32:55 -04:00
stub(@attribute).name { :name }
stub(@attribute).add_to
stub(@proxy).set
stub(@proxy).result { 'result' }
stub(FactoryGirl::Attribute::Static).new { @attribute }
stub(FactoryGirl::Proxy::Build).new { @proxy }
2009-04-11 11:27:23 -04:00
@factory.define_attribute(@attribute)
2009-04-11 11:27:23 -04:00
end
it "should create the right proxy using the build class when running" do
mock(FactoryGirl::Proxy::Build).new(@factory.build_class) { @proxy }
@factory.run(FactoryGirl::Proxy::Build, {})
2009-04-11 11:27:23 -04:00
end
it "should add the attribute to the proxy when running" do
mock(@attribute).add_to(@proxy)
@factory.run(FactoryGirl::Proxy::Build, {})
2009-04-11 11:27:23 -04:00
end
it "should return the result from the proxy when running" do
mock(@proxy).result() { 'result' }
@factory.run(FactoryGirl::Proxy::Build, {}).should == 'result'
2009-04-11 11:27:23 -04:00
end
end
2009-04-11 11:27:23 -04:00
it "should return associations" do
factory = FactoryGirl::Factory.new(:post)
factory.define_attribute(FactoryGirl::Attribute::Association.new(:author, :author, {}))
factory.define_attribute(FactoryGirl::Attribute::Association.new(:editor, :editor, {}))
factory.associations.each do |association|
association.should be_a(FactoryGirl::Attribute::Association)
2009-04-11 11:27:23 -04:00
end
factory.associations.size.should == 2
end
2009-04-11 11:27:23 -04:00
it "should raise for a self referencing association" do
factory = FactoryGirl::Factory.new(:post)
lambda {
factory.define_attribute(FactoryGirl::Attribute::Association.new(:parent, :post, {}))
}.should raise_error(FactoryGirl::AssociationDefinitionError)
end
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
it "should return the overridden value in the generated attributes" do
attr = FactoryGirl::Attribute::Static.new(@name, 'The price is wrong, Bob!')
@factory.define_attribute(attr)
result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
result[@name].should == @value
2009-04-11 11:27:23 -04:00
end
it "should not call a lazy attribute block for an overridden attribute" do
attr = FactoryGirl::Attribute::Dynamic.new(@name, lambda { flunk })
@factory.define_attribute(attr)
result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
2009-04-11 11:27:23 -04:00
end
it "should override a symbol parameter with a string parameter" do
attr = FactoryGirl::Attribute::Static.new(@name, 'The price is wrong, Bob!')
@factory.define_attribute(attr)
@hash = { @name.to_s => @value }
result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
result[@name].should == @value
2009-04-11 11:27:23 -04:00
end
end
2009-04-11 11:27:23 -04:00
describe "overriding an attribute with an alias" do
before do
@factory.define_attribute(FactoryGirl::Attribute::Static.new(:test, 'original'))
Factory.alias(/(.*)_alias/, '\1')
@result = @factory.run(FactoryGirl::Proxy::AttributesFor,
:test_alias => 'new')
2009-04-11 11:27:23 -04:00
end
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
it "should discard the predefined value for the attribute" do
@result[:test].should be_nil
2009-09-15 15:47:47 -04:00
end
end
2009-09-15 15:47: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
it "should create a new factory using the class of the parent" do
child = FactoryGirl::Factory.new(:child)
child.inherit_from(@factory)
child.build_class.should == @factory.build_class
end
2009-04-11 11:27:23 -04:00
it "should create a new factory while overriding the parent class" do
child = FactoryGirl::Factory.new(:child, :class => String)
child.inherit_from(@factory)
child.build_class.should == String
end
2009-04-11 11:27:23 -04:00
describe "given a parent with attributes" do
before do
@parent_attr = :name
@factory.define_attribute(FactoryGirl::Attribute::Static.new(@parent_attr, 'value'))
2009-04-11 11:27:23 -04:00
end
it "should create a new factory with attributes of the parent" do
child = FactoryGirl::Factory.new(:child)
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
it "should allow a child to define additional attributes" do
child = FactoryGirl::Factory.new(:child)
child.define_attribute(FactoryGirl::Attribute::Static.new(:email, 'value'))
child.inherit_from(@factory)
child.attributes.size.should == 2
2009-04-11 11:27:23 -04:00
end
it "should allow to override parent attributes" do
child = FactoryGirl::Factory.new(:child)
@child_attr = FactoryGirl::Attribute::Static.new(@parent_attr, 'value')
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
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
end
2009-04-11 11:27:23 -04:00
it "inherit all callbacks" do
@factory.add_callback(:after_stub) { |object| object.name = 'Stubby' }
child = FactoryGirl::Factory.new(:child)
child.inherit_from(@factory)
child.attributes.last.should be_kind_of(FactoryGirl::Attribute::Callback)
end
end
2009-04-11 11:27:23 -04:00
describe FactoryGirl::Factory, "when defined with a custom class" do
before do
@class = Float
@factory = FactoryGirl::Factory.new(:author, :class => @class)
end
2009-04-11 11:27:23 -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
describe FactoryGirl::Factory, "when defined with a class instead of a name" do
before do
@class = ArgumentError
@name = :argument_error
@factory = FactoryGirl::Factory.new(@class)
end
2009-04-11 11:27:23 -04:00
it "should guess the name from the class" do
@factory.name.should == @name
2009-04-11 11:27:23 -04:00
end
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
describe FactoryGirl::Factory, "when defined with a custom class name" do
before do
@class = ArgumentError
@factory = FactoryGirl::Factory.new(:author, :class => :argument_error)
end
2009-04-11 11:27:23 -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
end
2009-04-11 11:27:23 -04:00
describe FactoryGirl::Factory, "with a name ending in s" do
include DefinesConstants
2009-04-11 11:27:23 -04:00
before do
2010-11-11 16:33:45 -05:00
define_class('Business')
@name = :business
@class = Business
@factory = FactoryGirl::Factory.new(@name)
2009-04-11 11:27:23 -04:00
end
it "should have a factory name" do
@factory.name.should == @name
2009-04-11 11:27:23 -04:00
end
it "should have a build class" do
@factory.build_class.should == @class
end
end
describe FactoryGirl::Factory, "with a string for a name" do
before do
@name = :string
@factory = FactoryGirl::Factory.new(@name.to_s) {}
end
it "should convert the string to a symbol" do
@factory.name.should == @name
end
end
describe FactoryGirl::Factory, "registered with a string name" do
before do
@name = :string
@factory = FactoryGirl::Factory.new(@name)
FactoryGirl.register_factory(@factory)
end
2009-04-11 11:27:23 -04:00
it "should store the factory using a symbol" do
FactoryGirl.factories[@name].should == @factory
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
describe FactoryGirl::Factory, "for namespaced class" do
include DefinesConstants
2009-04-11 11:27:23 -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
@name = :settings
@class = Admin::Settings
end
2009-04-11 11:27:23 -04:00
it "should build namespaced class passed by string" do
factory = FactoryGirl::Factory.new(@name.to_s, :class => @class.name)
factory.build_class.should == @class
end
2009-04-11 11:27:23 -04:00
it "should build Admin::Settings class from Admin::Settings string" do
factory = FactoryGirl::Factory.new(@name.to_s, :class => 'admin/settings')
factory.build_class.should == @class
end
end
2009-04-11 11:27:23 -04:00
describe FactoryGirl::Factory do
include DefinesConstants
2009-04-11 11:27:23 -04:00
before do
2010-11-11 16:33:45 -05:00
define_class('User')
define_class('Admin', User)
end
2009-04-11 11:27:23 -04:00
it "should raise an ArgumentError when trying to use a non-existent strategy" do
lambda {
FactoryGirl::Factory.new(:object, :default_strategy => :nonexistent) {}
}.should raise_error(ArgumentError)
end
2009-04-11 11:27:23 -04:00
it "should create a new factory with a specified default strategy" do
factory = FactoryGirl::Factory.new(:object, :default_strategy => :stub)
factory.default_strategy.should == :stub
2009-04-11 11:27:23 -04:00
end
describe 'defining a child factory without setting default strategy' do
2009-04-11 11:27:23 -04:00
before do
@parent = FactoryGirl::Factory.new(:object, :default_strategy => :stub)
@child = FactoryGirl::Factory.new(:child_object)
@child.inherit_from(@parent)
2009-04-11 11:27:23 -04:00
end
it "should inherit default strategy from its parent" do
@child.default_strategy.should == :stub
end
2009-04-11 11:27:23 -04:00
end
describe 'defining a child factory with a default strategy' do
before do
@parent = FactoryGirl::Factory.new(:object, :default_strategy => :stub)
@child = FactoryGirl::Factory.new(:child_object2, :default_strategy => :build)
@child.inherit_from(@parent)
2009-04-11 11:27:23 -04:00
end
it "should override the default strategy from parent" do
@child.default_strategy.should == :build
end
2009-04-11 11:27:23 -04:00
end
end
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
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