Class: SpecForge::Attribute::Factory

Inherits:
Parameterized show all
Includes:
Chainable
Defined in:
lib/spec_forge/attribute/factory.rb

Overview

Represents an attribute that references a factory to generate test data

This class allows SpecForge to integrate with FactoryBot for test data generation. It supports various build strategies like create, build, build_stubbed, etc.

Examples:

Basic usage in YAML

user: factories.user

With custom attributes

user:
  factories.user:
    attributes:
      name: "Custom Name"
      email: faker.internet.email

With build strategy

user:
  factories.user:
    strategy: build
    attributes:
      admin: true

With an array of 5 user attributes

user:
  factories.user:
    strategy: attributes_for
    size: 5
    attributes:
      admin: true

Constant Summary collapse

KEYWORD_REGEX =

Regular expression pattern that matches attribute keywords with this prefix Used for identifying this attribute type during parsing

Returns:

  • (Regexp)
/^factories\./i
BASE_STRATEGIES =

An array of base strategies that can be provided either with or without a size. "stubbed" will automatically be transformed into "build_stubbed"

Returns:

%w[
  build
  create
  build_stubbed
  attributes_for
].freeze
BUILD_STRATEGIES =

Returns All available build strategies.

Returns:

  • (Array<String>)

    All available build strategies

%w[
  attributes_for
  attributes_for_list
  build
  build_list
  build_pair
  build_stubbed
  build_stubbed_list
  create
  create_list
  create_pair
].freeze

Constants included from Chainable

Chainable::NUMBER_REGEX

Instance Attribute Summary

Attributes included from Chainable

#header, #invocation_chain, #keyword

Attributes inherited from Parameterized

#arguments

Instance Method Summary collapse

Methods included from Chainable

#resolved, #value

Methods inherited from Parameterized

from_hash

Constructor Details

#initializeFactory

Creates a new factory attribute with the specified name and arguments



81
82
83
84
85
86
87
88
# File 'lib/spec_forge/attribute/factory.rb', line 81

def initialize(...)
  super

  # Check the arguments before preparing them
  arguments[:keyword] = Normalizer.normalize!(arguments[:keyword], using: :factory_reference)

  prepare_arguments
end

Instance Method Details

#base_objectObject

Returns the base object for the variable chain

Returns:

  • (Object)

    The result of the FactoryBot call



95
96
97
98
99
100
101
102
103
# File 'lib/spec_forge/attribute/factory.rb', line 95

def base_object
  attributes = arguments[:keyword]

  # Default functionality is to create ("factory.user")
  return FactoryBot.create(factory_name) if attributes.blank?

  build_arguments = construct_factory_parameters(attributes)
  FactoryBot.public_send(*build_arguments)
end

#resolveObject

Similar to #resolved but doesn't cache the result, allowing for re-resolution. Recursively calls #resolve on all nested attributes without storing results.

Use this when you need to ensure fresh values each time, particularly with factories or other attributes that should generate new values on each call.

Examples:

factory_attr = Attribute::Factory.new("factories.user")
factory_attr.resolve # => User#1 (a new user)
factory_attr.resolve # => User#2 (another new user)

Returns:

  • (Object)

    The completely resolved value without caching



119
120
121
122
123
124
125
126
127
128
# File 'lib/spec_forge/attribute/factory.rb', line 119

def resolve
  case value
  when Array
    value.map(&resolve_proc)
  when Hash
    value.transform_values(&resolve_proc)
  else
    value
  end
end