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/definition_spec.rb
Joshua Clayton 41bc3ac5ff Add FactoryGirl::Definition
Both Factory and Trait have similar methods and interact with a
DefinitionProxy. The idea here is to move the interface DefinitionProxy
expects to a separate class and both Factory and Trait can delegate to
an instance of Definition.
2011-10-30 23:17:03 -04:00

67 lines
1.9 KiB
Ruby

require "spec_helper"
describe FactoryGirl::Definition do
it { should delegate(:declare_attribute).to(:attribute_list) }
end
describe FactoryGirl::Definition, "with a name" do
let(:name) { :"great name" }
subject { FactoryGirl::Definition.new(name) }
it "creates a new attribute list with the name passed" do
FactoryGirl::AttributeList.stubs(:new)
subject
FactoryGirl::AttributeList.should have_received(:new).with(name)
end
end
describe FactoryGirl::Definition, "defining traits" do
let(:trait_1) { stub("trait") }
let(:trait_2) { stub("trait") }
it "maintains a list of traits" do
subject.define_trait(trait_1)
subject.define_trait(trait_2)
subject.defined_traits.should == [trait_1, trait_2]
end
end
describe FactoryGirl::Definition, "adding callbacks" do
let(:callback_1) { stub("callback") }
let(:callback_2) { stub("callback") }
it "maintains a list of callbacks" do
subject.add_callback(callback_1)
subject.add_callback(callback_2)
subject.callbacks.should == [callback_1, callback_2]
end
end
describe FactoryGirl::Definition, "#to_create" do
its(:to_create) { should be_nil }
it "returns the assigned value when given a block" do
block = proc { nil }
subject.to_create(&block)
subject.to_create.should == block
end
end
describe FactoryGirl::Definition, "#trait_by_name" do
let(:female_trait) { stub("female trait", :name => :female) }
let(:admin_trait) { stub("admin trait", :name => :admin) }
before do
subject.define_trait(female_trait)
end
it "finds the correct trait if defined on the definition" do
subject.trait_by_name(:female).should == female_trait
end
it "looks for the trait on FactoryGirl" do
FactoryGirl.stubs(:trait_by_name => admin_trait)
subject.trait_by_name(:admin).should == admin_trait
FactoryGirl.should have_received(:trait_by_name).with(:admin)
end
end