Last active
January 19, 2023 10:46
-
-
Save ikraamg/a55f9270bfa30d7d1ddfd1b2a53f8f04 to your computer and use it in GitHub Desktop.
How to use factory bot in the console and also receive the errors messages from failed creations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#In rails console: | |
include FactoryBot::Syntax::Methods | |
#From: https://github.com/thoughtbot/factory_bot/issues/397#issuecomment-6662051 | |
FactoryBot.define do | |
to_create do |instance| | |
unless instance.save | |
raise "Invalid record: " + instance.errors.messages.to_s | |
end | |
end | |
end | |
# Now can use create(:<factory_name>) and if it fails it will return the errors if it fails to create. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment