mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
parent
9fcd0b6b4e
commit
334b8a9449
8 changed files with 71 additions and 71 deletions
|
@ -19,11 +19,11 @@ describe "a blueprint" do
|
|||
@instance = FactoryGirl.create(:user, :last_name => 'Rye')
|
||||
end
|
||||
|
||||
it "should use attributes from the blueprint" do
|
||||
it "uses attributes from the blueprint" do
|
||||
@instance.first_name.should == 'Bill'
|
||||
end
|
||||
|
||||
it "should evaluate attribute blocks for each instance" do
|
||||
it "evaluates attribute blocks for each instance" do
|
||||
@instance.email.should =~ /somebody\d+@example.com/
|
||||
FactoryGirl.create(:user).email.should_not == @instance.email
|
||||
end
|
||||
|
|
|
@ -17,16 +17,16 @@ describe "a factory using generate syntax" do
|
|||
end
|
||||
end
|
||||
|
||||
it "should not raise an error when generating an invalid instance" do
|
||||
lambda { User.generate(:first_name => nil) }.should_not raise_error
|
||||
it "does not raise an error when generating an invalid instance" do
|
||||
expect { User.generate(:first_name => nil) }.to_not raise_error
|
||||
end
|
||||
|
||||
it "should raise an error when forcefully generating an invalid instance" do
|
||||
lambda { User.generate!(:first_name => nil) }.should raise_error(ActiveRecord::RecordInvalid)
|
||||
it "raises an error when forcefully generating an invalid instance" do
|
||||
expect { User.generate!(:first_name => nil) }.to raise_error(ActiveRecord::RecordInvalid)
|
||||
end
|
||||
|
||||
%w(generate generate! spawn).each do |method|
|
||||
it "should yield a generated instance when using #{method} with a block" do
|
||||
it "yields a generated instance when using #{method} with a block" do
|
||||
saved_instance = nil
|
||||
User.send(method) {|instance| saved_instance = instance }
|
||||
saved_instance.should be_kind_of(User)
|
||||
|
@ -37,20 +37,20 @@ describe "a factory using generate syntax" do
|
|||
@instance = User.send(method, :last_name => 'Rye')
|
||||
end
|
||||
|
||||
it "should use attributes from the factory" do
|
||||
it "uses attributes from the factory" do
|
||||
@instance.first_name.should == 'Bill'
|
||||
end
|
||||
|
||||
it "should use attributes passed to generate" do
|
||||
it "uses attributes passed to generate" do
|
||||
@instance.last_name.should == 'Rye'
|
||||
end
|
||||
|
||||
if method == 'spawn'
|
||||
it "should not save the record" do
|
||||
it "does not save the record" do
|
||||
@instance.should be_new_record
|
||||
end
|
||||
else
|
||||
it "should save the record" do
|
||||
it "does save the record" do
|
||||
@instance.should_not be_new_record
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,15 +19,15 @@ describe "a factory using make syntax" do
|
|||
@instance = User.make(:last_name => 'Rye')
|
||||
end
|
||||
|
||||
it "should use attributes from the factory" do
|
||||
it "uses attributes from the factory" do
|
||||
@instance.first_name.should == 'Bill'
|
||||
end
|
||||
|
||||
it "should use attributes passed to make" do
|
||||
it "uses attributes passed to make" do
|
||||
@instance.last_name.should == 'Rye'
|
||||
end
|
||||
|
||||
it "should build the record" do
|
||||
it "builds the record" do
|
||||
@instance.should be_new_record
|
||||
end
|
||||
end
|
||||
|
@ -37,15 +37,15 @@ describe "a factory using make syntax" do
|
|||
@instance = User.make!(:last_name => 'Rye')
|
||||
end
|
||||
|
||||
it "should use attributes from the factory" do
|
||||
it "uses attributes from the factory" do
|
||||
@instance.first_name.should == 'Bill'
|
||||
end
|
||||
|
||||
it "should use attributes passed to make" do
|
||||
it "uses attributes passed to make" do
|
||||
@instance.last_name.should == 'Rye'
|
||||
end
|
||||
|
||||
it "should save the record" do
|
||||
it "saves the record" do
|
||||
@instance.should_not be_new_record
|
||||
end
|
||||
end
|
||||
|
|
|
@ -28,15 +28,15 @@ describe "a factory using sham syntax" do
|
|||
@instance = FactoryGirl.create(:user, :last_name => 'Rye')
|
||||
end
|
||||
|
||||
it "should support a sham called 'name'" do
|
||||
it "supports a sham called 'name'" do
|
||||
@instance.first_name.should == 'Name'
|
||||
end
|
||||
|
||||
it "should support shams with starting values" do
|
||||
it "supports shams with starting values" do
|
||||
@instance.username.should == 'User-FOO'
|
||||
end
|
||||
|
||||
it "should use the sham for the email" do
|
||||
it "uses the sham for the email" do
|
||||
@instance.email.should =~ /somebody\d@example.com/
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,29 +29,29 @@ describe "vintage syntax" do
|
|||
end
|
||||
end
|
||||
|
||||
it "should raise an ArgumentError when trying to use a non-existent strategy" do
|
||||
lambda {
|
||||
it "raises an ArgumentError when trying to use a non-existent strategy" do
|
||||
expect {
|
||||
Factory.define(:object, :default_strategy => :nonexistent) {}
|
||||
}.should raise_error(ArgumentError)
|
||||
}.to raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "should raise Factory::SequenceAbuseError" do
|
||||
it "raises Factory::SequenceAbuseError" do
|
||||
Factory.define :sequence_abuser, :class => User do |factory|
|
||||
factory.first_name { Factory.sequence(:name) }
|
||||
end
|
||||
|
||||
lambda {
|
||||
expect {
|
||||
Factory(:sequence_abuser)
|
||||
}.should raise_error(FactoryGirl::SequenceAbuseError)
|
||||
}.to raise_error(FactoryGirl::SequenceAbuseError)
|
||||
end
|
||||
end
|
||||
|
||||
describe Factory, "referencing a nonexistent factory as a parent" do
|
||||
it "should raise an ArgumentError when trying to use a non-existent factory as parent" do
|
||||
lambda {
|
||||
it "raises an ArgumentError when trying to use a non-existent factory as parent" do
|
||||
expect {
|
||||
Factory.define(:child, :parent => :nonexsitent) {}
|
||||
Factory.build(:child)
|
||||
}.should raise_error(ArgumentError)
|
||||
}.to raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -65,13 +65,13 @@ describe "defining a factory" do
|
|||
FactoryGirl::DefinitionProxy.stubs(:new => @proxy)
|
||||
end
|
||||
|
||||
it "should create a new factory using the specified name and options" do
|
||||
it "creates a new factory using the specified name and options" do
|
||||
FactoryGirl::Factory.stubs(:new => @factory)
|
||||
Factory.define(@name, @options) {|f| }
|
||||
FactoryGirl::Factory.should have_received(:new).with(@name, @options)
|
||||
end
|
||||
|
||||
it "should pass the factory do the block" do
|
||||
it "passes the factory do the block" do
|
||||
yielded = nil
|
||||
Factory.define(@name) do |y|
|
||||
yielded = y
|
||||
|
@ -79,7 +79,7 @@ describe "defining a factory" do
|
|||
yielded.should == @proxy
|
||||
end
|
||||
|
||||
it "should add the factory to the list of factories" do
|
||||
it "adds the factory to the list of factories" do
|
||||
Factory.define(@name) {|f| }
|
||||
@factory.should == FactoryGirl.factory_by_name(@name)
|
||||
end
|
||||
|
@ -93,48 +93,48 @@ describe "after defining a factory" do
|
|||
FactoryGirl.register_factory(@factory)
|
||||
end
|
||||
|
||||
it "should use Proxy::AttributesFor for Factory.attributes_for" do
|
||||
it "uses Proxy::AttributesFor for Factory.attributes_for" do
|
||||
@factory.stubs(:run => "result")
|
||||
Factory.attributes_for(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::AttributesFor, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use Proxy::Build for Factory.build" do
|
||||
it "uses Proxy::Build for Factory.build" do
|
||||
@factory.stubs(:run => "result")
|
||||
Factory.build(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Build, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use Proxy::Create for Factory.create" do
|
||||
it "uses Proxy::Create for Factory.create" do
|
||||
@factory.stubs(:run => "result")
|
||||
Factory.create(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Create, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use Proxy::Stub for Factory.stub" do
|
||||
it "uses Proxy::Stub for Factory.stub" do
|
||||
@factory.stubs(:run => "result")
|
||||
Factory.stub(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Stub, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use default strategy option as Factory.default_strategy" do
|
||||
it "uses default strategy option as Factory.default_strategy" do
|
||||
@factory.stubs(:default_strategy => :create, :run => "result")
|
||||
Factory.default_strategy(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Create, :attr => 'value')
|
||||
end
|
||||
|
||||
it "should use the default strategy for the global Factory method" do
|
||||
it "uses the default strategy for the global Factory method" do
|
||||
@factory.stubs(:default_strategy => :create, :run => "result")
|
||||
Factory(@name, :attr => 'value').should == 'result'
|
||||
@factory.should have_received(:run).with(FactoryGirl::Proxy::Create, :attr => 'value')
|
||||
end
|
||||
|
||||
[:build, :create, :attributes_for, :stub].each do |method|
|
||||
it "should raise an ArgumentError on #{method} with a nonexistent factory" do
|
||||
lambda { Factory.send(method, :bogus) }.should raise_error(ArgumentError)
|
||||
it "raises an ArgumentError on #{method} with a nonexistent factory" do
|
||||
expect { Factory.send(method, :bogus) }.to raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "should recognize either 'name' or :name for Factory.#{method}" do
|
||||
it "recognizes either 'name' or :name for Factory.#{method}" do
|
||||
@factory.stubs(:run)
|
||||
lambda { Factory.send(method, @name.to_s) }.should_not raise_error
|
||||
lambda { Factory.send(method, @name.to_sym) }.should_not raise_error
|
||||
|
@ -147,17 +147,17 @@ describe "defining a sequence" do
|
|||
@name = :count
|
||||
end
|
||||
|
||||
it "should create a new sequence" do
|
||||
it "creates a new sequence" do
|
||||
Factory.sequence(@name)
|
||||
Factory.next(@name).should == 1
|
||||
end
|
||||
|
||||
it "should use the supplied block as the sequence generator" do
|
||||
it "uses the supplied block as the sequence generator" do
|
||||
Factory.sequence(@name) {|n| "user-#{n}" }
|
||||
Factory.next(@name).should == "user-1"
|
||||
end
|
||||
|
||||
it "should use the supplied start_value as the sequence start_value" do
|
||||
it "uses the supplied start_value as the sequence start_value" do
|
||||
Factory.sequence(@name, "A")
|
||||
Factory.next(@name).should == "A"
|
||||
end
|
||||
|
@ -175,12 +175,12 @@ describe "after defining a sequence" do
|
|||
Factory.sequence(@name) {}
|
||||
end
|
||||
|
||||
it "should call next on the sequence when sent next" do
|
||||
it "calls next on the sequence when sent next" do
|
||||
Factory.next(@name)
|
||||
@sequence.should have_received(:next)
|
||||
end
|
||||
|
||||
it "should return the value from the sequence" do
|
||||
it "returns the value from the sequence" do
|
||||
Factory.next(@name).should == @value
|
||||
end
|
||||
end
|
||||
|
@ -196,7 +196,7 @@ describe "an attribute generated by an in-line sequence" do
|
|||
@username = Factory.attributes_for(:user)[:username]
|
||||
end
|
||||
|
||||
it "should match the correct format" do
|
||||
it "matches the correct format" do
|
||||
@username.should =~ /^username\d+$/
|
||||
end
|
||||
|
||||
|
@ -205,11 +205,11 @@ describe "an attribute generated by an in-line sequence" do
|
|||
@another_username = Factory.attributes_for(:user)[:username]
|
||||
end
|
||||
|
||||
it "should match the correct format" do
|
||||
it "matches the correct format" do
|
||||
@username.should =~ /^username\d+$/
|
||||
end
|
||||
|
||||
it "should not be the same as the first generated value" do
|
||||
it "is not the same as the first generated value" do
|
||||
@another_username.should_not == @username
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ describe FactoryGirl::DefinitionProxy do
|
|||
let(:factory) { FactoryGirl::Factory.new(:object) }
|
||||
subject { FactoryGirl::DefinitionProxy.new(factory) }
|
||||
|
||||
it "should add a static attribute when an attribute is defined with a value" do
|
||||
it "adds a static attribute when an attribute is defined with a value" do
|
||||
attribute = stub('attribute', :name => :name)
|
||||
FactoryGirl::Attribute::Static.stubs(:new => attribute)
|
||||
factory.stubs(:define_attribute)
|
||||
|
@ -14,7 +14,7 @@ describe FactoryGirl::DefinitionProxy do
|
|||
FactoryGirl::Attribute::Static.should have_received(:new).with(:name, "value", false)
|
||||
end
|
||||
|
||||
it "should add a dynamic attribute when an attribute is defined with a block" do
|
||||
it "adds a dynamic attribute when an attribute is defined with a block" do
|
||||
attribute = stub('attribute', :name => :name)
|
||||
block = lambda {}
|
||||
FactoryGirl::Attribute::Dynamic.stubs(:new => attribute)
|
||||
|
@ -25,16 +25,16 @@ describe FactoryGirl::DefinitionProxy do
|
|||
factory.should have_received(:define_attribute).with(attribute)
|
||||
end
|
||||
|
||||
it "should raise for an attribute with a value and a block" do
|
||||
lambda {
|
||||
it "raises for an attribute with a value and a block" do
|
||||
expect {
|
||||
subject.add_attribute(:name, 'value') {}
|
||||
}.should raise_error(FactoryGirl::AttributeDefinitionError)
|
||||
}.to raise_error(FactoryGirl::AttributeDefinitionError)
|
||||
end
|
||||
|
||||
describe "child factories" do
|
||||
its(:child_factories) { should == [] }
|
||||
|
||||
it "should be able to add child factories" do
|
||||
it "is be able to add child factories" do
|
||||
block = lambda {}
|
||||
subject.factory(:admin, { :aliases => [:great] }, &block)
|
||||
subject.child_factories.should == [[:admin, { :aliases => [:great] }, block]]
|
||||
|
@ -42,13 +42,13 @@ describe FactoryGirl::DefinitionProxy do
|
|||
end
|
||||
|
||||
describe "adding an attribute using a in-line sequence" do
|
||||
it "should create the sequence" do
|
||||
it "creates the sequence" do
|
||||
FactoryGirl::Sequence.stubs(:new)
|
||||
subject.sequence(:name) {}
|
||||
FactoryGirl::Sequence.should have_received(:new).with(:name, 1)
|
||||
end
|
||||
|
||||
it "should create the sequence with a custom default value" do
|
||||
it "creates the sequence with a custom default value" do
|
||||
FactoryGirl::Sequence.stubs(:new)
|
||||
subject.sequence(:name, "A") {}
|
||||
FactoryGirl::Sequence.should have_received(:new).with(:name, "A")
|
||||
|
|
|
@ -8,7 +8,7 @@ describe FactoryGirl::Factory do
|
|||
FactoryGirl.register_factory(@factory)
|
||||
end
|
||||
|
||||
it "should have a factory name" do
|
||||
it "has a factory name" do
|
||||
@factory.name.should == @name
|
||||
end
|
||||
|
||||
|
@ -16,11 +16,11 @@ describe FactoryGirl::Factory do
|
|||
@factory.factory_name.should == @name
|
||||
end
|
||||
|
||||
it "should have a build class" do
|
||||
it "has a build class" do
|
||||
@factory.build_class.should == @class
|
||||
end
|
||||
|
||||
it "should have a default strategy" do
|
||||
it "has a default strategy" do
|
||||
@factory.default_strategy.should == :create
|
||||
end
|
||||
|
||||
|
@ -36,7 +36,7 @@ describe FactoryGirl::Factory do
|
|||
proxy.should have_received(:result).with(block)
|
||||
end
|
||||
|
||||
it "should return associations" do
|
||||
it "returns associations" do
|
||||
factory = FactoryGirl::Factory.new(:post)
|
||||
FactoryGirl.register_factory(FactoryGirl::Factory.new(:admin))
|
||||
factory.declare_attribute(FactoryGirl::Declaration::Association.new(:author, {}))
|
||||
|
@ -48,7 +48,7 @@ describe FactoryGirl::Factory do
|
|||
factory.associations.size.should == 3
|
||||
end
|
||||
|
||||
it "should raise for a self referencing association" do
|
||||
it "raises for a self referencing association" do
|
||||
factory = FactoryGirl::Factory.new(:post)
|
||||
lambda {
|
||||
factory.declare_attribute(FactoryGirl::Declaration::Association.new(:parent, { :factory => :post }))
|
||||
|
@ -63,20 +63,20 @@ describe FactoryGirl::Factory do
|
|||
@hash = { @name => @value }
|
||||
end
|
||||
|
||||
it "should return the overridden value in the generated attributes" do
|
||||
it "returns the overridden value in the generated attributes" do
|
||||
declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
|
||||
@factory.declare_attribute(declaration)
|
||||
result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
|
||||
result[@name].should == @value
|
||||
end
|
||||
|
||||
it "should not call a lazy attribute block for an overridden attribute" do
|
||||
it "does not call a lazy attribute block for an overridden attribute" do
|
||||
declaration = FactoryGirl::Declaration::Dynamic.new(@name, lambda { flunk })
|
||||
@factory.declare_attribute(declaration)
|
||||
result = @factory.run(FactoryGirl::Proxy::AttributesFor, @hash)
|
||||
end
|
||||
|
||||
it "should override a symbol parameter with a string parameter" do
|
||||
it "overrides a symbol parameter with a string parameter" do
|
||||
declaration = FactoryGirl::Declaration::Static.new(@name, 'The price is wrong, Bob!')
|
||||
@factory.declare_attribute(declaration)
|
||||
@hash = { @name.to_s => @value }
|
||||
|
@ -93,26 +93,26 @@ describe FactoryGirl::Factory do
|
|||
:test_alias => 'new')
|
||||
end
|
||||
|
||||
it "should use the passed in value for the alias" do
|
||||
it "uses the passed in value for the alias" do
|
||||
@result[:test_alias].should == 'new'
|
||||
end
|
||||
|
||||
it "should discard the predefined value for the attribute" do
|
||||
it "discards the predefined value for the attribute" do
|
||||
@result[:test].should be_nil
|
||||
end
|
||||
end
|
||||
|
||||
it "should guess the build class from the factory name" do
|
||||
it "guesses the build class from the factory name" do
|
||||
@factory.build_class.should == User
|
||||
end
|
||||
|
||||
it "should create a new factory using the class of the parent" do
|
||||
it "creates a new factory using the class of the parent" do
|
||||
child = FactoryGirl::Factory.new(:child, :parent => @factory.name)
|
||||
child.ensure_compiled
|
||||
child.build_class.should == @factory.build_class
|
||||
end
|
||||
|
||||
it "should create a new factory while overriding the parent class" do
|
||||
it "creates a new factory while overriding the parent class" do
|
||||
child = FactoryGirl::Factory.new(:child, :class => String, :parent => @factory.name)
|
||||
child.ensure_compiled
|
||||
child.build_class.should == String
|
||||
|
|
|
@ -78,7 +78,7 @@ describe "definition loading" do
|
|||
describe "with several factories files under #{dir}/factories in non-alphabetical order" do
|
||||
in_directory_with_files File.join(dir, 'factories', 'b.rb'),
|
||||
File.join(dir, 'factories', 'a.rb')
|
||||
it "should load the files in the right order" do
|
||||
it "loads the files in the right order" do
|
||||
FactoryGirl.stubs(:load)
|
||||
sorted_load_order = sequence("load order")
|
||||
FactoryGirl.expects(:load).with(includes("a.rb")).in_sequence(sorted_load_order)
|
||||
|
|
Loading…
Reference in a new issue