Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

New default syntax for using defined factories

  • Loading branch information...
commit 7c90e9d370eadd3c7224e7b531919dccc84d01a7 1 parent fc6d1b3
@jferris jferris authored
View
63 README.md
@@ -67,38 +67,36 @@ Using factories
factory_girl supports several different build strategies: build, create, attributes_for and stub:
# Returns a User instance that's not saved
- user = Factory.build(:user)
+ user = FactoryGirl.build(:user)
# Returns a saved User instance
- user = Factory.create(:user)
+ user = FactoryGirl.create(:user)
# Returns a hash of attributes that can be used to build a User instance:
- attrs = Factory.attributes_for(:user)
+ attrs = FactoryGirl.attributes_for(:user)
# Returns an object with all defined attributes stubbed out:
- stub = Factory.stub(:user)
-
-You can use the Factory method as a shortcut for the default build strategy:
-
- # Same as Factory.create :user:
- user = Factory(:user)
-
-The default strategy can be overriden:
-
- # Now same as Factory.build(:user)
- factory :user, :default_strategy => :build do
- ...
- end
-
- user = Factory(:user)
+ stub = FactoryGirl.stub(:user)
No matter which strategy is used, it's possible to override the defined attributes by passing a hash:
# Build a User instance and override the first_name property
- user = Factory.build(:user, :first_name => 'Joe')
+ user = FactoryGirl.build(:user, :first_name => 'Joe')
user.first_name
# => "Joe"
+If repeating "FactoryGirl" is too verbose for you, you can mix the syntax methods in:
+
+ # rspec
+ RSpec.configure do |config|
+ config.include Factory::Syntax::Methods
+ end
+
+ # Test::Unit
+ class Test::Unit::TestCase
+ include Factory::Syntax::Methods
+ end
+
Lazy Attributes
---------------
@@ -119,8 +117,8 @@ Attributes can be based on the values of other attributes using the proxy that i
last_name 'Blow'
email { "#{first_name}.#{last_name}@example.com".downcase }
end
-
- Factory(:user, :last_name => 'Doe').email
+
+ FactoryGirl.create(:user, :last_name => 'Doe').email
# => "joe.doe@example.com"
Associations
@@ -144,12 +142,12 @@ You can also specify a different factory or override attributes:
The behavior of the association method varies depending on the build strategy used for the parent object.
# Builds and saves a User and a Post
- post = Factory(:post)
+ post = FactoryGirl.create(:post)
post.new_record? # => false
post.author.new_record # => false
# Builds and saves a User, and then builds but does not save a Post
- post = Factory.build(:post)
+ post = FactoryGirl.build(:post)
post.new_record? # => true
post.author.new_record # => false
@@ -176,12 +174,15 @@ Sequences
---------
Unique values in a specific format (for example, e-mail addresses) can be
-generated using sequences. Sequences are defined by calling Factory.sequence,
-and values in a sequence are generated by calling Factory.next:
+generated using sequences. Sequences are defined by calling sequence in a
+definition block, and values in a sequence are generated by calling
+Factory.next:
# Defines a new sequence
- FactoryGirl.sequence :email do |n|
- "person#{n}@example.com"
+ FactoryGirl.define do
+ sequence :email do |n|
+ "person#{n}@example.com"
+ end
end
Factory.next :email
@@ -214,9 +215,9 @@ Callbacks
Factory_girl makes available three callbacks for injecting some code:
-* after_build - called after a factory is built (via Factory.build)
-* after_create - called after a factory is saved (via Factory.create)
-* after_stub - called after a factory is stubbed (via Factory.stub)
+* after_build - called after a factory is built (via FactoryGirl.build)
+* after_create - called after a factory is saved (via FactoryGirl.create)
+* after_stub - called after a factory is stubbed (via FactoryGirl.stub)
Examples:
@@ -241,7 +242,7 @@ Factories can also define any number of the same kind of callback. These callba
after_create { then_this }
end
-Calling Factory.create will invoke both after_build and after_create callbacks.
+Calling FactoryGirl.create will invoke both after_build and after_create callbacks.
Also, like standard attributes, child factories will inherit (and can define additional) callbacks from their parent factory.
View
1  lib/factory_girl.rb
@@ -12,6 +12,7 @@
require 'factory_girl/sequence'
require 'factory_girl/aliases'
require 'factory_girl/definition_proxy'
+require 'factory_girl/syntax/methods'
require 'factory_girl/syntax/default'
require 'factory_girl/syntax/vintage'
require 'factory_girl/find_definitions'
View
2  lib/factory_girl/syntax/default.rb
@@ -1,6 +1,8 @@
module FactoryGirl
module Syntax
module Default
+ include Methods
+
def define(&block)
DSL.run(block)
end
View
75 lib/factory_girl/syntax/methods.rb
@@ -0,0 +1,75 @@
+module FactoryGirl
+ module Syntax
+ module Methods
+ # Generates and returns a Hash of attributes from this factory. Attributes
+ # can be individually overridden by passing in a Hash of attribute => value
+ # pairs.
+ #
+ # Arguments:
+ # * name: +Symbol+ or +String+
+ # The name of the factory that should be used.
+ # * overrides: +Hash+
+ # Attributes to overwrite for this set.
+ #
+ # Returns: +Hash+
+ # A set of attributes that can be used to build an instance of the class
+ # this factory generates.
+ def attributes_for(name, overrides = {})
+ FactoryGirl.factory_by_name(name).run(Proxy::AttributesFor, overrides)
+ end
+
+ # Generates and returns an instance from this factory. Attributes can be
+ # individually overridden by passing in a Hash of attribute => value pairs.
+ #
+ # Arguments:
+ # * name: +Symbol+ or +String+
+ # The name of the factory that should be used.
+ # * overrides: +Hash+
+ # Attributes to overwrite for this instance.
+ #
+ # Returns: +Object+
+ # An instance of the class this factory generates, with generated attributes
+ # assigned.
+ def build(name, overrides = {})
+ FactoryGirl.factory_by_name(name).run(Proxy::Build, overrides)
+ end
+
+ # Generates, saves, and returns an instance from this factory. Attributes can
+ # be individually overridden by passing in a Hash of attribute => value
+ # pairs.
+ #
+ # Instances are saved using the +save!+ method, so ActiveRecord models will
+ # raise ActiveRecord::RecordInvalid exceptions for invalid attribute sets.
+ #
+ # Arguments:
+ # * name: +Symbol+ or +String+
+ # The name of the factory that should be used.
+ # * overrides: +Hash+
+ # Attributes to overwrite for this instance.
+ #
+ # Returns: +Object+
+ # A saved instance of the class this factory generates, with generated
+ # attributes assigned.
+ def create(name, overrides = {})
+ FactoryGirl.factory_by_name(name).run(Proxy::Create, overrides)
+ end
+
+ # Generates and returns an object with all attributes from this factory
+ # stubbed out. Attributes can be individually overridden by passing in a Hash
+ # of attribute => value pairs.
+ #
+ # Arguments:
+ # * name: +Symbol+ or +String+
+ # The name of the factory that should be used.
+ # * overrides: +Hash+
+ # Attributes to overwrite for this instance.
+ #
+ # Returns: +Object+
+ # An object with generated attributes stubbed out.
+ def build_stubbed(name, overrides = {})
+ FactoryGirl.factory_by_name(name).run(Proxy::Stub, overrides)
+ end
+
+ end
+ end
+end
View
76 lib/factory_girl/syntax/vintage.rb
@@ -2,6 +2,8 @@ module FactoryGirl
module Syntax
module Vintage
module Factory
+ extend Syntax::Methods
+
# Defines a new factory that can be used by the build strategies (create and
# build) to build new objects.
#
@@ -33,75 +35,6 @@ def self.define(name, options = {})
FactoryGirl.register_factory(factory)
end
- # Generates and returns a Hash of attributes from this factory. Attributes
- # can be individually overridden by passing in a Hash of attribute => value
- # pairs.
- #
- # Arguments:
- # * name: +Symbol+ or +String+
- # The name of the factory that should be used.
- # * overrides: +Hash+
- # Attributes to overwrite for this set.
- #
- # Returns: +Hash+
- # A set of attributes that can be used to build an instance of the class
- # this factory generates.
- def self.attributes_for(name, overrides = {})
- FactoryGirl.factory_by_name(name).run(Proxy::AttributesFor, overrides)
- end
-
- # Generates and returns an instance from this factory. Attributes can be
- # individually overridden by passing in a Hash of attribute => value pairs.
- #
- # Arguments:
- # * name: +Symbol+ or +String+
- # The name of the factory that should be used.
- # * overrides: +Hash+
- # Attributes to overwrite for this instance.
- #
- # Returns: +Object+
- # An instance of the class this factory generates, with generated attributes
- # assigned.
- def self.build(name, overrides = {})
- FactoryGirl.factory_by_name(name).run(Proxy::Build, overrides)
- end
-
- # Generates, saves, and returns an instance from this factory. Attributes can
- # be individually overridden by passing in a Hash of attribute => value
- # pairs.
- #
- # Instances are saved using the +save!+ method, so ActiveRecord models will
- # raise ActiveRecord::RecordInvalid exceptions for invalid attribute sets.
- #
- # Arguments:
- # * name: +Symbol+ or +String+
- # The name of the factory that should be used.
- # * overrides: +Hash+
- # Attributes to overwrite for this instance.
- #
- # Returns: +Object+
- # A saved instance of the class this factory generates, with generated
- # attributes assigned.
- def self.create(name, overrides = {})
- FactoryGirl.factory_by_name(name).run(Proxy::Create, overrides)
- end
-
- # Generates and returns an object with all attributes from this factory
- # stubbed out. Attributes can be individually overridden by passing in a Hash
- # of attribute => value pairs.
- #
- # Arguments:
- # * name: +Symbol+ or +String+
- # The name of the factory that should be used.
- # * overrides: +Hash+
- # Attributes to overwrite for this instance.
- #
- # Returns: +Object+
- # An object with generated attributes stubbed out.
- def self.stub(name, overrides = {})
- FactoryGirl.factory_by_name(name).run(Proxy::Stub, overrides)
- end
-
# Executes the default strategy for the given factory. This is usually create,
# but it can be overridden for each factory.
#
@@ -180,6 +113,11 @@ def self.alias(pattern, replace)
FactoryGirl.aliases << [pattern, replace]
end
+ # Alias for build_stubbed. Deprecated.
+ def self.stub(name, overrides = {})
+ build_stubbed(name, overrides)
+ end
+
end
# Shortcut for Factory.default_strategy.
View
2  spec/acceptance/attribute_aliases_spec.rb
@@ -20,7 +20,7 @@
end
it "doesn't assign both an association and its foreign key" do
- Factory.build(:post, :user_id => 1).user_id.should == 1
+ FactoryGirl.build(:post, :user_id => 1).user_id.should == 1
end
end
View
4 spec/acceptance/attributes_for_spec.rb
@@ -2,6 +2,8 @@
require 'acceptance/acceptance_helper'
describe "a generated attributes hash" do
+ include FactoryGirl::Syntax::Methods
+
before do
define_model('User')
@@ -25,7 +27,7 @@
end
end
- subject { Factory.attributes_for(:post, :title => 'overridden title') }
+ subject { attributes_for(:post, :title => 'overridden title') }
it "assigns an overridden value" do
subject[:title].should == "overridden title"
View
4 spec/acceptance/build_spec.rb
@@ -2,6 +2,8 @@
require 'acceptance/acceptance_helper'
describe "a built instance" do
+ include FactoryGirl::Syntax::Methods
+
before do
define_model('User')
@@ -19,7 +21,7 @@
end
end
- subject { Factory.build(:post) }
+ subject { build(:post) }
it "isn't saved" do
should be_new_record
View
6 spec/acceptance/stub_spec.rb → spec/acceptance/build_stubbed_spec.rb
@@ -2,6 +2,8 @@
require 'acceptance/acceptance_helper'
describe "a generated stub instance" do
+ include FactoryGirl::Syntax::Methods
+
before do
define_model('User')
@@ -23,7 +25,7 @@
end
end
- subject { Factory.stub(:post, :title => 'overridden title') }
+ subject { build_stubbed(:post, :title => 'overridden title') }
it "assigns a default attribute" do
subject.body.should == 'default body'
@@ -42,7 +44,7 @@
end
it "generates unique ids" do
- other_stub = Factory.stub(:post)
+ other_stub = build_stubbed(:post)
subject.id.should_not == other_stub.id
end
View
8 spec/acceptance/callbacks_spec.rb
@@ -19,23 +19,23 @@
end
it "runs the after_stub callback when stubbing" do
- user = Factory.stub(:user_with_callbacks)
+ user = FactoryGirl.build_stubbed(:user_with_callbacks)
user.first_name.should == 'Stubby'
end
it "runs the after_build callback when building" do
- user = Factory.build(:user_with_callbacks)
+ user = FactoryGirl.build(:user_with_callbacks)
user.first_name.should == 'Buildy'
end
it "runs both the after_build and after_create callbacks when creating" do
- user = Factory(:user_with_callbacks)
+ user = FactoryGirl.create(:user_with_callbacks)
user.first_name.should == 'Buildy'
user.last_name.should == 'Createy'
end
it "runs both the after_stub callback on the factory and the inherited after_stub callback" do
- user = Factory.stub(:user_with_inherited_callbacks)
+ user = FactoryGirl.build_stubbed(:user_with_inherited_callbacks)
user.first_name.should == 'Stubby'
user.last_name.should == 'Double-Stubby'
end
View
6 spec/acceptance/create_spec.rb
@@ -2,6 +2,8 @@
require 'acceptance/acceptance_helper'
describe "a created instance" do
+ include FactoryGirl::Syntax::Methods
+
before do
define_model('User')
@@ -19,7 +21,7 @@
end
end
- subject { Factory.create('post') }
+ subject { create('post') }
it "saves" do
should_not be_new_record
@@ -32,6 +34,8 @@
end
describe "a custom create" do
+ include FactoryGirl::Syntax::Methods
+
before do
define_class('User') do
def initialize
View
2  spec/acceptance/definition_spec.rb
@@ -15,7 +15,7 @@
end
end
- subject { Factory.create(:admin) }
+ subject { FactoryGirl.create(:admin) }
it "uses the correct class name" do
should be_kind_of(User)
View
2  spec/acceptance/parent_spec.rb
@@ -18,7 +18,7 @@
end
end
- subject { Factory.create(:admin) }
+ subject { FactoryGirl.create(:admin) }
it "uses the parent build class" do
subject.should be_kind_of(User)
Please sign in to comment.
Something went wrong with that request. Please try again.