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
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(@factory.name).to eq @name
|
2010-10-02 00:00:58 -04:00
|
|
|
end
|
|
|
|
|
2011-10-12 14:32:23 -04:00
|
|
|
it "has a build class" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(@factory.build_class).to eq @class
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2010-11-12 16:21:16 -05:00
|
|
|
it "passes a custom creation block" do
|
2017-06-01 12:54:02 -04:00
|
|
|
strategy = double("strategy", result: nil, add_observer: true)
|
|
|
|
allow(FactoryGirl::Strategy::Build).to receive(:new).and_return strategy
|
2012-04-20 16:59:39 -04:00
|
|
|
block = -> {}
|
2010-11-12 16:21:16 -05:00
|
|
|
factory = FactoryGirl::Factory.new(:object)
|
|
|
|
factory.to_create(&block)
|
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
factory.run(FactoryGirl::Strategy::Build, {})
|
2010-11-12 16:21:16 -05:00
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(strategy).to have_received(:result).with(instance_of(FactoryGirl::Evaluation))
|
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|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(association).to be_association
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(factory.associations.to_a.length).to eq 3
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2012-02-07 18:55:57 -05:00
|
|
|
it "includes associations from the parent factory" do
|
2012-02-08 00:23:04 -05:00
|
|
|
association_on_parent = FactoryGirl::Declaration::Association.new(:association_on_parent, {})
|
|
|
|
association_on_child = FactoryGirl::Declaration::Association.new(:association_on_child, {})
|
|
|
|
|
2012-02-07 18:55:57 -05:00
|
|
|
factory = FactoryGirl::Factory.new(:post)
|
2012-02-08 00:23:04 -05:00
|
|
|
factory.declare_attribute(association_on_parent)
|
2012-02-07 18:55:57 -05:00
|
|
|
FactoryGirl.register_factory(factory)
|
2012-02-08 00:23:04 -05:00
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
child_factory = FactoryGirl::Factory.new(:child_post, parent: :post)
|
2012-02-08 00:23:04 -05:00
|
|
|
child_factory.declare_attribute(association_on_child)
|
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(child_factory.associations.map(&:name)).to eq [:association_on_parent, :association_on_child]
|
2012-02-07 18:55:57 -05:00
|
|
|
end
|
|
|
|
|
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)
|
2012-02-08 10:17:57 -05:00
|
|
|
result = @factory.run(FactoryGirl::Strategy::AttributesFor, @hash)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(result[@name]).to eq @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
|
2012-04-20 16:59:39 -04:00
|
|
|
declaration = FactoryGirl::Declaration::Dynamic.new(@name, false, -> { flunk })
|
2011-09-23 13:14:02 -04:00
|
|
|
@factory.declare_attribute(declaration)
|
2012-02-08 10:17:57 -05:00
|
|
|
@factory.run(FactoryGirl::Strategy::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 }
|
2012-02-08 10:17:57 -05:00
|
|
|
result = @factory.run(FactoryGirl::Strategy::AttributesFor, @hash)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(result[@name]).to eq @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'))
|
2012-03-09 17:45:42 -05:00
|
|
|
FactoryGirl.aliases << [/(.*)_alias/, '\1']
|
2012-02-08 10:17:57 -05:00
|
|
|
@result = @factory.run(FactoryGirl::Strategy::AttributesFor,
|
2012-03-09 17:20:38 -05: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
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(@result[:test_alias]).to eq '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
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(@result[:test]).to 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
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(@factory.build_class).to eq User
|
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 "creates a new factory using the class of the parent" do
|
2012-03-09 17:20:38 -05:00
|
|
|
child = FactoryGirl::Factory.new(:child, parent: @factory.name)
|
2011-10-28 10:59:49 -04:00
|
|
|
child.compile
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(child.build_class).to eq @factory.build_class
|
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 "creates a new factory while overriding the parent class" do
|
2012-03-09 17:20:38 -05:00
|
|
|
child = FactoryGirl::Factory.new(:child, class: String, parent: @factory.name)
|
2011-10-28 10:59:49 -04:00
|
|
|
child.compile
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(child.build_class).to eq String
|
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
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { FactoryGirl::Factory.new(:author, class: Float) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:build_class) { should eq Float }
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2011-12-14 17:10:32 -05:00
|
|
|
describe FactoryGirl::Factory, "when given a class that overrides #to_s" do
|
|
|
|
let(:overriding_class) { Overriding::Class }
|
|
|
|
|
|
|
|
before do
|
|
|
|
define_class("Overriding")
|
|
|
|
define_class("Overriding::Class") do
|
|
|
|
def self.to_s
|
|
|
|
"Overriding"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { FactoryGirl::Factory.new(:overriding_class, class: Overriding::Class) }
|
2011-12-14 17:10:32 -05:00
|
|
|
|
|
|
|
it "sets build_class correctly" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.build_class).to eq overriding_class
|
2011-12-14 17:10:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq name }
|
|
|
|
its(:build_class) { should eq 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
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { FactoryGirl::Factory.new(:author, class: :argument_error) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:build_class) { should eq 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
|
|
|
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq name }
|
|
|
|
its(:build_class) { should eq 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) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq 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
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { FactoryGirl::Factory.new(name, class: "Admin::Settings") }
|
2011-08-13 01:03:12 -04:00
|
|
|
|
|
|
|
it "sets build_class correctly" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.build_class).to eq settings_class
|
2011-08-13 01:03:12 -04:00
|
|
|
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
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { FactoryGirl::Factory.new(name, class: "admin/settings") }
|
2011-08-13 01:03:12 -04:00
|
|
|
|
|
|
|
it "sets build_class correctly" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.build_class).to eq settings_class
|
2011-08-13 01:03:12 -04:00
|
|
|
end
|
2010-06-10 14:58:47 -04:00
|
|
|
end
|
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
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) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:names) { should eq [:user] }
|
|
|
|
its(:human_names) { should eq ["user"] }
|
2011-06-30 18:27:25 -04:00
|
|
|
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) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:names) { should eq [:happy_user] }
|
|
|
|
its(:human_names) { should eq ["happy user"] }
|
2010-10-02 00:00:58 -04:00
|
|
|
end
|
|
|
|
|
2011-10-27 15:45:21 -04:00
|
|
|
context "factory name with big letters" do
|
|
|
|
subject { FactoryGirl::Factory.new(:LoL) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:names) { should eq [:LoL] }
|
|
|
|
its(:human_names) { should eq ["lol"] }
|
2011-10-27 15:45:21 -04:00
|
|
|
end
|
|
|
|
|
2011-06-30 18:27:25 -04:00
|
|
|
context "factory name with aliases" do
|
2012-03-09 17:20:38 -05:00
|
|
|
subject { FactoryGirl::Factory.new(:happy_user, aliases: [:gleeful_user, :person]) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:names) { should eq [:happy_user, :gleeful_user, :person] }
|
|
|
|
its(:human_names) { should eq ["happy user", "gleeful user", "person"] }
|
2011-06-30 18:27:25 -04:00
|
|
|
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) }
|
2017-06-01 12:54:02 -04:00
|
|
|
let(:strategy) do
|
|
|
|
double("strategy", result: "result", add_observer: true)
|
|
|
|
end
|
2011-09-23 13:14:02 -04:00
|
|
|
let(:attributes) { [attribute] }
|
2017-06-01 12:54:02 -04:00
|
|
|
let(:attribute_list) do
|
|
|
|
double("attribute-list", declarations: [declaration], to_a: attributes)
|
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
|
|
|
|
before do
|
2012-03-09 17:20:38 -05:00
|
|
|
define_model("User", name: :string)
|
2017-06-01 12:54:02 -04:00
|
|
|
allow(FactoryGirl::Declaration::Static).to receive(:new).
|
|
|
|
and_return declaration
|
|
|
|
allow(declaration).to receive(:to_attributes).and_return attributes
|
|
|
|
allow(FactoryGirl::Strategy::Build).to receive(:new).and_return strategy
|
2011-09-23 13:14:02 -04:00
|
|
|
subject.declare_attribute(declaration)
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
it "creates the right strategy using the build class when running" do
|
|
|
|
subject.run(FactoryGirl::Strategy::Build, {})
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(FactoryGirl::Strategy::Build).to have_received(:new).once
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|
|
|
|
|
2012-02-08 10:17:57 -05:00
|
|
|
it "returns the result from the strategy when running" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.run(FactoryGirl::Strategy::Build, {})).to eq "result"
|
2011-08-20 17:08:15 -04:00
|
|
|
end
|
2011-10-26 23:51:25 -04:00
|
|
|
|
|
|
|
it "calls the block and returns the result" do
|
|
|
|
block_run = nil
|
2012-04-20 16:59:39 -04:00
|
|
|
block = ->(result) { block_run = "changed" }
|
2012-02-08 10:17:57 -05:00
|
|
|
subject.run(FactoryGirl::Strategy::Build, { }, &block)
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(block_run).to eq "changed"
|
2011-10-26 23:51:25 -04:00
|
|
|
end
|
2011-08-20 01:09:29 -04:00
|
|
|
end
|