2017-10-20 15:20:28 -04:00
|
|
|
module FactoryBot
|
2011-01-19 19:47:49 -05:00
|
|
|
module Syntax
|
2012-05-04 12:05:31 -04:00
|
|
|
## This module is a container for all strategy methods provided by
|
2017-10-20 15:20:28 -04:00
|
|
|
## FactoryBot. This includes all the default strategies provided ({Methods#build},
|
2019-01-11 10:32:36 -05:00
|
|
|
## {Methods#create}, {Methods#build_stubbed}, and {Methods#attributes_for}), as
|
|
|
|
## well as the complementary *_list and *_pair methods.
|
2012-05-04 12:05:31 -04:00
|
|
|
## @example singular factory execution
|
|
|
|
## # basic use case
|
|
|
|
## build(:completed_order)
|
|
|
|
##
|
|
|
|
## # factory yielding its result to a block
|
|
|
|
## create(:post) do |post|
|
|
|
|
## create(:comment, post: post)
|
|
|
|
## end
|
|
|
|
##
|
|
|
|
## # factory with attribute override
|
|
|
|
## attributes_for(:post, title: "I love Ruby!")
|
|
|
|
##
|
|
|
|
## # factory with traits and attribute override
|
|
|
|
## build_stubbed(:user, :admin, :male, name: "John Doe")
|
|
|
|
##
|
|
|
|
## @example multiple factory execution
|
|
|
|
## # basic use case
|
|
|
|
## build_list(:completed_order, 2)
|
|
|
|
## create_list(:completed_order, 2)
|
|
|
|
##
|
|
|
|
## # factory with attribute override
|
|
|
|
## attributes_for_list(:post, 4, title: "I love Ruby!")
|
|
|
|
##
|
|
|
|
## # factory with traits and attribute override
|
|
|
|
## build_stubbed_list(:user, 15, :admin, :male, name: "John Doe")
|
2011-01-19 19:47:49 -05:00
|
|
|
module Methods
|
2019-04-26 15:48:37 -04:00
|
|
|
# @!parse FactoryBot::Internal.register_default_strategies
|
2012-05-04 12:05:31 -04:00
|
|
|
# @!method build(name, *traits_and_overrides, &block)
|
|
|
|
# (see #strategy_method)
|
|
|
|
# Builds a registered factory by name.
|
|
|
|
# @return [Object] instantiated object defined by the factory
|
2011-01-04 16:12:04 -05:00
|
|
|
|
2012-05-04 12:05:31 -04:00
|
|
|
# @!method create(name, *traits_and_overrides, &block)
|
|
|
|
# (see #strategy_method)
|
|
|
|
# Creates a registered factory by name.
|
|
|
|
# @return [Object] instantiated object defined by the factory
|
|
|
|
|
|
|
|
# @!method build_stubbed(name, *traits_and_overrides, &block)
|
|
|
|
# (see #strategy_method)
|
|
|
|
# Builds a stubbed registered factory by name.
|
|
|
|
# @return [Object] instantiated object defined by the factory
|
|
|
|
|
|
|
|
# @!method attributes_for(name, *traits_and_overrides, &block)
|
|
|
|
# (see #strategy_method)
|
|
|
|
# Generates a hash of attributes for a registered factory by name.
|
|
|
|
# @return [Hash] hash of attributes for the factory
|
|
|
|
|
2019-01-11 10:32:36 -05:00
|
|
|
# @!method build_list(name, amount, *traits_and_overrides, &block)
|
2012-05-04 12:05:31 -04:00
|
|
|
# (see #strategy_method_list)
|
|
|
|
# @return [Array] array of built objects defined by the factory
|
|
|
|
|
2019-01-11 10:32:36 -05:00
|
|
|
# @!method create_list(name, amount, *traits_and_overrides, &block)
|
2012-05-04 12:05:31 -04:00
|
|
|
# (see #strategy_method_list)
|
|
|
|
# @return [Array] array of created objects defined by the factory
|
|
|
|
|
2019-01-11 10:32:36 -05:00
|
|
|
# @!method build_stubbed_list(name, amount, *traits_and_overrides, &block)
|
2012-05-04 12:05:31 -04:00
|
|
|
# (see #strategy_method_list)
|
|
|
|
# @return [Array] array of stubbed objects defined by the factory
|
|
|
|
|
2019-01-11 10:32:36 -05:00
|
|
|
# @!method attributes_for_list(name, amount, *traits_and_overrides, &block)
|
2012-05-04 12:05:31 -04:00
|
|
|
# (see #strategy_method_list)
|
|
|
|
# @return [Array<Hash>] array of attribute hashes for the factory
|
|
|
|
|
2019-01-11 10:32:36 -05:00
|
|
|
# @!method build_pair(name, *traits_and_overrides, &block)
|
|
|
|
# (see #strategy_method_pair)
|
|
|
|
# @return [Array] pair of built objects defined by the factory
|
|
|
|
|
|
|
|
# @!method create_pair(name, *traits_and_overrides, &block)
|
|
|
|
# (see #strategy_method_pair)
|
|
|
|
# @return [Array] pair of created objects defined by the factory
|
|
|
|
|
|
|
|
# @!method build_stubbed_pair(name, *traits_and_overrides, &block)
|
|
|
|
# (see #strategy_method_pair)
|
|
|
|
# @return [Array] pair of stubbed objects defined by the factory
|
|
|
|
|
|
|
|
# @!method attributes_for_pair(name, *traits_and_overrides, &block)
|
|
|
|
# (see #strategy_method_pair)
|
|
|
|
# @return [Array<Hash>] pair of attribute hashes for the factory
|
|
|
|
|
2012-05-04 12:05:31 -04:00
|
|
|
# @!method strategy_method
|
|
|
|
# @!visibility private
|
2016-12-16 05:29:38 -05:00
|
|
|
# @param [Symbol] name the name of the factory to build
|
2012-05-04 12:05:31 -04:00
|
|
|
# @param [Array<Symbol, Symbol, Hash>] traits_and_overrides splat args traits and a hash of overrides
|
|
|
|
# @param [Proc] block block to be executed
|
|
|
|
|
|
|
|
# @!method strategy_method_list
|
|
|
|
# @!visibility private
|
2016-12-16 05:29:38 -05:00
|
|
|
# @param [Symbol] name the name of the factory to execute
|
2012-05-04 12:05:31 -04:00
|
|
|
# @param [Integer] amount the number of instances to execute
|
|
|
|
# @param [Array<Symbol, Symbol, Hash>] traits_and_overrides splat args traits and a hash of overrides
|
2019-01-11 10:32:36 -05:00
|
|
|
# @param [Proc] block block to be executed
|
|
|
|
|
|
|
|
# @!method strategy_method_pair
|
|
|
|
# @!visibility private
|
|
|
|
# @param [Symbol] name the name of the factory to execute
|
|
|
|
# @param [Array<Symbol, Symbol, Hash>] traits_and_overrides splat args traits and a hash of overrides
|
|
|
|
# @param [Proc] block block to be executed
|
2011-01-04 16:12:04 -05:00
|
|
|
|
2011-05-19 10:56:45 -04:00
|
|
|
# Generates and returns the next value in a sequence.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# name: (Symbol)
|
|
|
|
# The name of the sequence that a value should be generated for.
|
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# The next value in the sequence. (Object)
|
|
|
|
def generate(name)
|
2019-04-26 15:48:37 -04:00
|
|
|
Internal.sequence_by_name(name).next
|
2011-05-19 10:56:45 -04:00
|
|
|
end
|
2016-12-16 05:18:31 -05:00
|
|
|
|
|
|
|
# Generates and returns the list of values in a sequence.
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# name: (Symbol)
|
|
|
|
# The name of the sequence that a value should be generated for.
|
|
|
|
# count: (Fixnum)
|
|
|
|
# Count of values
|
|
|
|
#
|
|
|
|
# Returns:
|
|
|
|
# The next value in the sequence. (Object)
|
|
|
|
def generate_list(name, count)
|
|
|
|
(1..count).map do
|
2019-04-26 15:48:37 -04:00
|
|
|
Internal.sequence_by_name(name).next
|
2016-12-16 05:18:31 -05:00
|
|
|
end
|
|
|
|
end
|
2011-01-19 19:47:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|