Out of the box, factory_bot can build non-persisted instances of Sequel models by calling
FactoryBot.build(:symbol)
. It just can't create them and save them in a single method call with FactoryBot.create(:symbol)
. In fact, if you try calling factory_bot's create method, you will get an error message that looks like this:NoMethodError: undefined method `save!' for #<Model:...>
There are two ways around this:
- Call
FactoryBot.build(:symbol)
to build a non-persisted Model instance and then call that instance's create() method - Modify your FactoryBot definitions to specify a different method to call on create()
to_create { |i| i.save }
2018-08-12 Edit: 'factory_girl' was renamed to 'factory_bot' in 2017 and I found out about it today :-) Also updated the link to Sequel.
3 comments:
Awesome, thanks!
This was _exactly_ what I was looking for. Thanks.
Thanks a billion
Post a Comment