Class: FactoryBot::Blueprint::RSpec::Letbp

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_bot/blueprint/rspec/letbp.rb

Overview

An intermediate object for letbp syntax. See Driver#letbp for more details.

Instance Method Summary collapse

Instance Method Details

#build { ... } ⇒ Object

Create a new blueprint, and create a set of objects (with build build strategy) from it.

Examples:

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

Yields:

  • Write Blueprint DSL code here



37
38
39
40
41
# File 'lib/factory_bot/blueprint/rspec/letbp.rb', line 37

def build(&)
  let_blueprint(:new, &)
  let_instance(:build)
  let_objects
end

#build_from { ... } ⇒ Object

Create a set of objects (with build build strategy) from an existing blueprint.

Examples:

let(:blog_blueprint) do
  bp.plan do
    blog(title: "Daily log") do
      let.article(title: "Article 1")
      article(title: "Article 2")
      article(title: "Article 3")
    end
  end
end
letbp(:blog, %i[article]).build_from { blog_blueprint }

Yields:

  • Usual let context for retrieving an existing blueprint



72
73
74
75
76
# File 'lib/factory_bot/blueprint/rspec/letbp.rb', line 72

def build_from(&)
  let_blueprint(:from, &)
  let_instance(:build)
  let_objects
end

#build_stubbed { ... } ⇒ Object

Create a new blueprint, and create a set of objects (with build_stubbed build strategy) from it.

Yields:

  • Write Blueprint DSL code here



45
46
47
48
49
# File 'lib/factory_bot/blueprint/rspec/letbp.rb', line 45

def build_stubbed(&)
  let_blueprint(:new, &)
  let_instance(:build_stubbed)
  let_objects
end

#build_stubbed_from { ... } ⇒ Object

Create a set of objects (with build_stubbed build strategy) from an existing blueprint.

Yields:

  • Usual let context for retrieving an existing blueprint



80
81
82
83
84
# File 'lib/factory_bot/blueprint/rspec/letbp.rb', line 80

def build_stubbed_from(&)
  let_blueprint(:from, &)
  let_instance(:build_stubbed)
  let_objects
end

#create { ... } ⇒ Object

Create a new blueprint, and create a set of objects (with create build strategy) from it.

Yields:

  • Write Blueprint DSL code here



53
54
55
56
57
# File 'lib/factory_bot/blueprint/rspec/letbp.rb', line 53

def create(&)
  let_blueprint(:new, &)
  let_instance(:create)
  let_objects
end

#create_from { ... } ⇒ Object

Create a set of objects (with create build strategy) from an existing blueprint.

Yields:

  • Usual let context for retrieving an existing blueprint



88
89
90
91
92
# File 'lib/factory_bot/blueprint/rspec/letbp.rb', line 88

def create_from(&)
  let_blueprint(:from, &)
  let_instance(:create)
  let_objects
end

#inherit { ... } ⇒ Object

Extend super() blueprint.

Examples:

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

  context "with a comment on the article" do
    letbp(:blog, %i[comment]).inherit do
      on.article do
        let.comment(text: "Comment")
      end
    end
  end
end

Yields:

  • Write Blueprint DSL code here



112
113
114
115
116
# File 'lib/factory_bot/blueprint/rspec/letbp.rb', line 112

def inherit(&)
  let_blueprint(:inherit, &)
  # We don't need `let_instance` here because it's already defined in the parent context
  let_objects
end