Skip to content

Sharp corner in usage with FactoryBot #7

@smudge

Description

@smudge

Given this state machine:

steady_state :status do
  state 'active', default: true
  state 'inactive', from: 'active'
end

We discovered that a factory will be invalid if it explicitly sets the "active" state in the default factory:

# factory
status { "active" }

# usage
FactoryBot.create(:my_factory)
#!> ActiveRecord::RecordInvalid: Validation failed: Status is invalid

FactoryBot.create(:my_factory, status: 'active')
#!> ActiveRecord::RecordInvalid: Validation failed: Status is invalid

FactoryBot.create(:my_factory, status: 'inactive')
#!> works

The fix is to remove the status { "active" } from the factory

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions