Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sharp corner in usage with FactoryBot #7

Open
smudge opened this issue Nov 1, 2019 · 2 comments
Open

Sharp corner in usage with FactoryBot #7

smudge opened this issue Nov 1, 2019 · 2 comments

Comments

@smudge
Copy link
Member

smudge commented Nov 1, 2019

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

@kylenakano
Copy link

kylenakano commented Nov 17, 2022

Looks like the fix only works if the factory status has the default status included in the from param when defining status. For example:

steady_state :status do
  state 'active', default: true
  state 'inactive', from: 'active'
  state 'canceled', from: 'inactive'
end
FactoryBot.create(:my_factory, status: 'canceled')
#!> ActiveRecord::RecordInvalid: Validation failed: Status is invalid

Any recommendations to workaround this case?

@smudge
Copy link
Member Author

smudge commented Nov 21, 2022

Any recommendations to workaround this case?

Circling back after resolving this offline -- it turns out that #6 was the culprit here. There may be something we can do to detect default values on columns and warn the developer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants