Module: FactoryBot::Blueprint

Defined in:
lib/factory_bot/blueprint.rb,
lib/factory_bot/blueprint/dsl.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 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

Classes: DSL

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

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

Create a set of objects by 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



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

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

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

Create a set of objects by create 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



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

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` 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 methods for DSL details

Returns:

  • (Factrey::Blueprint)

    the built or extended blueprint



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

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