Module: FactoryBot::Blueprint::RSpec::Driver

Included in:
RSpec::Core::ExampleGroup
Defined in:
lib/factory_bot/blueprint/rspec/driver.rb

Overview

Helper methods to integrate factory_bot-blueprint with RSpec. This module is automatically extended to RSpec::Core::ExampleGroup.

Instance Method Summary collapse

Instance Method Details

#letbp(name, items = []) ⇒ Letbp

This method expresses that the names given as arguments will be used in the let declaration for the set of objects to be created from the blueprint. Subsequent method calls specify specifically how to use FactoryBot to create the set of objects from the blueprint.

Examples:

RSpec.describe "something" do
  letbp(:blog, %i[article]).build do
    blog(title: "Daily log") do
      let.article(title: "Article 1")
      article(title: "Article 2")
      article(title: "Article 3")
    end
  end

  # Above example will be expanded to ...

  # Create a blueprint:
  let(:_letbp_blog_blueprint) do
    FactoryBot::Blueprint.plan(ext: self) do
      blog(title: "Daily log") do
        let.article(title: "Article 1")
        article(title: "Article 2")
        article(title: "Article 3")
      end
    end
  end

  # Create a set of objects (with `build` build strategy) from it:
  let(:_letbp_blog_instance) { FactoryBot::Blueprint.build(_letbp_blog_blueprint) }

  # Declare the result object:
  let(:blog) { _letbp_blog_instance[Factrey::Blueprint::Node::RESULT_NAME] }

  # Declare the named objects:
  let(:article) { _letbp_blog_instance[:article] }
end

Parameters:

  • name (Symbol)

    name of the result object to be declared using RSpec’s let

  • items (Array<Symbol>) (defaults to: [])

    names of the objects to be declared using RSpec’s let

Returns:



48
# File 'lib/factory_bot/blueprint/rspec/driver.rb', line 48

def letbp(name, items = []) = Letbp.new(self, :lazy, name, items)

#letbp!(name, items = []) ⇒ Letbp

let! version of #letbp.

Parameters:

  • name (Symbol)
  • items (Array<Symbol>) (defaults to: [])

Returns:



54
# File 'lib/factory_bot/blueprint/rspec/driver.rb', line 54

def letbp!(name, items = []) = Letbp.new(self, :eager, name, items)

#letbp_it_be(name, items = [], **options) ⇒ Letbp

let_it_be version of #letbp. This requires test-prof.

Parameters:

  • name (Symbol)
  • items (Array<Symbol>) (defaults to: [])
  • options (Hash)

    options for let_it_be

Returns:



61
62
63
# File 'lib/factory_bot/blueprint/rspec/driver.rb', line 61

def letbp_it_be(name, items = [], **options)
  Letbp.new(self, :let_it_be, name, items, let_it_be_options: options)
end