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

Updated docs and examples for new syntax

This commit is contained in:
Joe Ferris 2011-01-26 20:55:06 -05:00
parent 6c2322a11d
commit a7f7e85187
12 changed files with 24 additions and 23 deletions

View file

@ -127,7 +127,7 @@ module FactoryGirl
# # ... # # ...
# end # end
# #
# Factory(:author).class # FactoryGirl.create(:author).class
# # => User # # => User
# #
# Because an attribute defined without a value or block will build an # Because an attribute defined without a value or block will build an
@ -142,7 +142,7 @@ module FactoryGirl
# author # author
# end # end
# #
# Factory(:post).author.class # FactoryGirl.create(:post).author.class
# # => User # # => User
def aliases def aliases
@options[:aliases] || [] @options[:aliases] || []

View file

@ -56,11 +56,11 @@ module FactoryGirl
# end # end
# #
# # Builds (but doesn't save) a Post and a User # # Builds (but doesn't save) a Post and a User
# Factory.build(:post) # FactoryGirl.build(:post)
# #
# # Builds and saves a User, builds a Post, assigns the User to the # # Builds and saves a User, builds a Post, assigns the User to the
# # author association, and saves the User. # # author association, and saves the User.
# Factory.create(:post) # FactoryGirl.create(:post)
# #
def association(name, overrides = {}) def association(name, overrides = {})
nil nil

View file

@ -3,8 +3,8 @@ module FactoryGirl
# Raised when calling Factory.sequence from a dynamic attribute block # Raised when calling Factory.sequence from a dynamic attribute block
class SequenceAbuseError < StandardError; end class SequenceAbuseError < StandardError; end
# Sequences are defined using Factory.sequence. Sequence values are generated # Sequences are defined using sequence within a FactoryGirl.define block.
# using next. # Sequence values are generated using next.
class Sequence class Sequence
def initialize(value = 1, &proc) #:nodoc: def initialize(value = 1, &proc) #:nodoc:

View file

@ -12,7 +12,7 @@ module FactoryGirlStepHelpers
end end
model_class = factory.build_class model_class = factory.build_class
model_class.find(:first, :conditions => attributes_find) or model_class.find(:first, :conditions => attributes_find) or
Factory(factory_name, attributes) FactoryGirl.create(factory_name, attributes)
end end
def convert_human_hash_to_attribute_hash(human_hash, associations = []) def convert_human_hash_to_attribute_hash(human_hash, associations = [])
@ -37,22 +37,22 @@ FactoryGirl.factories.values.each do |factory|
end end
Given /^an? #{factory.human_name} exists$/ do Given /^an? #{factory.human_name} exists$/ do
Factory(factory.name) FactoryGirl.create(factory.name)
end end
Given /^(\d+) #{factory.human_name.pluralize} exist$/ do |count| Given /^(\d+) #{factory.human_name.pluralize} exist$/ do |count|
count.to_i.times { Factory(factory.name) } count.to_i.times { FactoryGirl.create(factory.name) }
end end
if factory.build_class.respond_to?(:columns) if factory.build_class.respond_to?(:columns)
factory.build_class.columns.each do |column| factory.build_class.columns.each do |column|
human_column_name = column.name.downcase.gsub('_', ' ') human_column_name = column.name.downcase.gsub('_', ' ')
Given /^an? #{factory.human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value| Given /^an? #{factory.human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
Factory(factory.name, column.name => value) FactoryGirl.create(factory.name, column.name => value)
end end
Given /^(\d+) #{factory.human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value| Given /^(\d+) #{factory.human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
count.to_i.times { Factory(factory.name, column.name => value) } count.to_i.times { FactoryGirl.create(factory.name, column.name => value) }
end end
end end
end end

View file

@ -13,7 +13,7 @@ module FactoryGirl
# email { 'billy@bob.example.com' } # email { 'billy@bob.example.com' }
# end # end
# #
# Factory(:user, :name => 'Johnny') # FactoryGirl.create(:user, :name => 'Johnny')
# #
# This syntax was derived from Pete Yandell's machinist. # This syntax was derived from Pete Yandell's machinist.
module Blueprint module Blueprint

View file

@ -15,14 +15,14 @@ module FactoryGirl
# end # end
# #
# # Creates a saved instance without raising (same as saving the result # # Creates a saved instance without raising (same as saving the result
# # of Factory.build) # # of FactoryGirl.build)
# User.generate(:name => 'Johnny') # User.generate(:name => 'Johnny')
# #
# # Creates a saved instance and raises when invalid (same as # # Creates a saved instance and raises when invalid (same as
# # Factory.create) # # FactoryGirl.create)
# User.generate! # User.generate!
# #
# # Creates an unsaved instance (same as Factory.build) # # Creates an unsaved instance (same as FactoryGirl.build)
# User.spawn # User.spawn
# #
# # Creates an instance and yields it to the passed block # # Creates an instance and yields it to the passed block

View file

@ -2,7 +2,7 @@ module FactoryGirl
module Syntax module Syntax
# Extends ActiveRecord::Base to provide a make class method, which is a # Extends ActiveRecord::Base to provide a make class method, which is a
# shortcut for Factory.create. # shortcut for FactoryGirl.create.
# #
# Usage: # Usage:
# #

View file

@ -20,6 +20,7 @@ module FactoryGirl
# factory will be copied to the current one with an ability to override # factory will be copied to the current one with an ability to override
# them. # them.
# * default_strategy: +Symbol+ # * default_strategy: +Symbol+
# DEPRECATED.
# The strategy that will be used by the Factory shortcut method. # The strategy that will be used by the Factory shortcut method.
# Defaults to :create. # Defaults to :create.
# #

View file

@ -61,7 +61,7 @@ describe "a custom create" do
end end
it "uses the custom create block instead of save" do it "uses the custom create block instead of save" do
Factory(:user).should be_persisted FactoryGirl.create(:user).should be_persisted
end end
end end

View file

@ -17,7 +17,7 @@ describe "a blueprint" do
describe "after making an instance" do describe "after making an instance" do
before do before do
@instance = Factory(:user, :last_name => 'Rye') @instance = FactoryGirl.create(:user, :last_name => 'Rye')
end end
it "should use attributes from the blueprint" do it "should use attributes from the blueprint" do
@ -26,7 +26,7 @@ describe "a blueprint" do
it "should evaluate attribute blocks for each instance" do it "should evaluate attribute blocks for each instance" do
@instance.email.should =~ /somebody\d+@example.com/ @instance.email.should =~ /somebody\d+@example.com/
Factory(:user).email.should_not == @instance.email FactoryGirl.create(:user).email.should_not == @instance.email
end end
end end
end end

View file

@ -26,7 +26,7 @@ describe "a factory using sham syntax" do
describe "after making an instance" do describe "after making an instance" do
before do before do
@instance = Factory(:user, :last_name => 'Rye') @instance = FactoryGirl.create(:user, :last_name => 'Rye')
end end
it "should support a sham called 'name'" do it "should support a sham called 'name'" do

View file

@ -7,7 +7,7 @@ describe FactoryGirl::Proxy::AttributesFor do
describe "when asked to associate with another factory" do describe "when asked to associate with another factory" do
before do before do
stub(Factory).create stub(FactoryGirl).create
@proxy.associate(:owner, :user, {}) @proxy.associate(:owner, :user, {})
end end
@ -21,9 +21,9 @@ describe FactoryGirl::Proxy::AttributesFor do
end end
it "should not call Factory.create when building an association" do it "should not call Factory.create when building an association" do
stub(Factory).create stub(FactoryGirl).create
@proxy.association(:user).should be_nil @proxy.association(:user).should be_nil
Factory.should have_received.create.never FactoryGirl.should have_received.create.never
end end
it "should always return nil when building an association" do it "should always return nil when building an association" do