module FactoryBot::Syntax::Methods

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")

Public Instance Methods

generate(name) click to toggle source

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)
# File lib/factory_bot/syntax/methods.rb, line 113
def generate(name)
  Internal.sequence_by_name(name).next
end
generate_list(name, count) click to toggle source

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)
# File lib/factory_bot/syntax/methods.rb, line 127
def generate_list(name, count)
  (1..count).map do
    Internal.sequence_by_name(name).next
  end
end