Module: FactoryBot::Blueprint

Defined in:
lib/factory_bot/blueprint.rb,
lib/factory_bot/blueprint/dsl.rb,
lib/factory_bot/blueprint/methods.rb,
lib/factory_bot/blueprint/version.rb

Overview

A FactoryBot extension for building structured objects using a declarative DSL. First we can build (or extend) a creation plan for a set of objects as Factrey::Blueprint, and then we can create actual objects from it.

Blueprints can be built using a declarative DSL provided by a core library called Factrey. Each node declaration in the DSL code is automatically correspond to the FactoryBot’s factory. For example, a declaration user(name: 'John') corresponds to FactoryBot.create(:user, name: 'John').

Defined Under Namespace

Modules: Methods Classes: DSL

Constant Summary collapse

VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.build(blueprint = nil, ext: nil) { ... } ⇒ Hash{Symbol => Object}

Create a set of objects by build build strategy in FactoryBot. See plan for more details.

Parameters:

  • blueprint (Factrey::Blueprint, nil) (defaults to: nil)
  • ext (Object) (defaults to: nil)

    an external object that can be accessed using DSL#ext in the DSL

Yields:

  • Write Blueprint DSL code here

Returns:

  • (Hash{Symbol => Object})

    the created objects



67
# File 'lib/factory_bot/blueprint.rb', line 67

def build(blueprint = nil, ext: nil, &) = instantiate(:build, blueprint, ext:, &)

.build_stubbed(blueprint = nil, ext: nil) { ... } ⇒ Hash{Symbol => Object}

Create a set of objects by build_stubbed build strategy in FactoryBot. See plan for more details.

Parameters:

  • blueprint (Factrey::Blueprint, nil) (defaults to: nil)
  • ext (Object) (defaults to: nil)

    an external object that can be accessed using DSL#ext in the DSL

Yields:

  • Write Blueprint DSL code here

Returns:

  • (Hash{Symbol => Object})

    the created objects



75
# File 'lib/factory_bot/blueprint.rb', line 75

def build_stubbed(blueprint = nil, ext: nil, &) = instantiate(:build_stubbed, blueprint, ext:, &)

.create(blueprint = nil, ext: nil) { ... } ⇒ Hash{Symbol => Object}

Create a set of objects by create build strategy in FactoryBot. See plan for more details.

Parameters:

  • blueprint (Factrey::Blueprint, nil) (defaults to: nil)
  • ext (Object) (defaults to: nil)

    an external object that can be accessed using DSL#ext in the DSL

Yields:

  • Write Blueprint DSL code here

Returns:

  • (Hash{Symbol => Object})

    the created objects



83
# File 'lib/factory_bot/blueprint.rb', line 83

def create(blueprint = nil, ext: nil, &) = instantiate(:create, blueprint, ext:, &)

.plan(blueprint = nil, ext: nil) { ... } ⇒ Factrey::Blueprint

Entry point to build or extend a Factrey::Blueprint.

Examples:

# In this example, we have three factories in FactoryBot:
FactoryBot.define do
  factory(:blog)
  factory(:article) { association :blog }
  factory(:comment) { association :article }
end

bp =
  FactoryBot::Blueprint.plan do
    let.blog do
      article(title: "Article 1")
      article(title: "Article 2")
      article(title: "Article 3") do
        comment(name: "John")
        comment(name: "Doe")
      end
    end
  end

# Create a set of objects in FactoryBot (with `build` build strategy) from the blueprint:
objects = FactoryBot::Blueprint.build(bp)

# This behaves as:
objects = {}
objects[:blog] = blog = FactoryBot.build(:blog)
objects[random_anon_sym] = FactoryBot.build(:article, title: "Article 1", blog:)
objects[random_anon_sym] = FactoryBot.build(:article, title: "Article 2", blog:)
objects[random_anon_sym] = article3 = FactoryBot.build(:article, title: "Article 3", blog:)
objects[random_anon_sym] = FactoryBot.build(:comment, name: "John", article: article3)
objects[random_anon_sym] = FactoryBot.build(:comment, name: "Doe", article: article3)
objects[Factrey::Blueprint::Node::RESULT_NAME] = blog

Parameters:

  • blueprint (Factrey::Blueprint, nil) (defaults to: nil)

    to extend an existing blueprint

  • ext (Object) (defaults to: nil)

    an external object that can be accessed using DSL#ext in the DSL

Yields:

  • Write Blueprint DSL code here. See Factrey::DSL documents for more details

Returns:

  • (Factrey::Blueprint)

    the built or extended blueprint



59
# File 'lib/factory_bot/blueprint.rb', line 59

def plan(blueprint = nil, ext: nil, &) = Factrey.blueprint(blueprint, ext:, dsl: DSL, &)