1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
module FactoryBot
module Syntax
## This module is a container for all strategy methods provided by
## FactoryBot. This includes all the default strategies provided ({Methods#build},
## {Methods#create}, {Methods#build_stubbed}, and {Methods#attributes_for}), as
## well as the complementary *_list and *_pair methods.
## @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")
module Methods
# @!parse FactoryBot::Internal.register_default_strategies
# @!method build(name, *traits_and_overrides, &block)
# (see #strategy_method)
# Builds a registered factory by name.
# @return [Object] instantiated object defined by the factory
# @!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
# @!method build_list(name, amount, *traits_and_overrides, &block)
# (see #strategy_method_list)
# @return [Array] array of built objects defined by the factory
# @!method create_list(name, amount, *traits_and_overrides, &block)
# (see #strategy_method_list)
# @return [Array] array of created objects defined by the factory
# @!method build_stubbed_list(name, amount, *traits_and_overrides, &block)
# (see #strategy_method_list)
# @return [Array] array of stubbed objects defined by the factory
# @!method attributes_for_list(name, amount, *traits_and_overrides, &block)
# (see #strategy_method_list)
# @return [Array<Hash>] array of attribute hashes for the factory
# @!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
# @!method strategy_method(name, traits_and_overrides, &block)
# @!visibility private
# @param [Symbol] name the name of the factory to build
# @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(name, amount, traits_and_overrides, &block)
# @!visibility private
# @param [Symbol] name the name of the factory to execute
# @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
# @param [Proc] block block to be executed
# @!method strategy_method_pair(name, traits_and_overrides, &block)
# @!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
# Generates and returns the next value in a global or factory sequence.
#
# Arguments:
# context: (Array of Symbols)
# The definition context of the sequence, with the sequence name
# as the final entry
# scope: (object)(optional)
# The object the sequence should be evaluated within
#
# Returns:
# The next value in the sequence. (Object)
#
# Example:
# generate(:my_factory, :my_trair, :my_sequence)
#
def generate(*uri_parts, scope: nil)
uri = FactoryBot::UriManager.build_uri(uri_parts)
sequence = Sequence.find_by_uri(uri) ||
raise(KeyError,
"Sequence not registered: #{FactoryBot::UriManager.build_uri(uri_parts)}")
increment_sequence(sequence, scope: scope)
end
# Generates and returns the list of values in a global or factory sequence.
#
# Arguments:
# uri_parts: (Array of Symbols)
# The definition context of the sequence, with the sequence name
# as the final entry
# scope: (object)(optional)
# The object the sequence should be evaluated within
#
# Returns:
# The next value in the sequence. (Object)
#
# Example:
# generate_list(:my_factory, :my_trair, :my_sequence, 5)
#
def generate_list(*uri_parts, count, scope: nil)
uri = FactoryBot::UriManager.build_uri(uri_parts)
sequence = Sequence.find_by_uri(uri) ||
raise(KeyError, "Sequence not registered: '#{uri}'")
(1..count).map do
increment_sequence(sequence, scope: scope)
end
end
# ======================================================================
# = PRIVATE
# ======================================================================
#
private
##
# Increments the given sequence and returns the value.
#
# Arguments:
# sequence:
# The sequence instance
# scope: (object)(optional)
# The object the sequence should be evaluated within
#
def increment_sequence(sequence, scope: nil)
value = sequence.next(scope)
raise if value.respond_to?(:start_with?) && value.start_with?("#<FactoryBot::Declaration")
value
rescue
raise ArgumentError, "Sequence '#{sequence.uri_manager.first}' failed to " \
"return a value. Perhaps it needs a scope to operate? (scope: <object>)"
end
end
end
end
|