Rspec / FactoryGirl: changes to factory not saved to test database?

I have the following factory defined for the model:

factory :page do association :user, factory: :standard_user association :post, factory: [:short_post] after :create do |model| model.post.user = model.user model.save! end end 

after creating the block, the file seems to be launched, and the factory returns the model object with the correct / new changes, however this is not saved in my test database.

that is, if I call Page.last.post.user.id from my test, I still get the old user ID that was assigned to the post factory object before the block after creation. I am not sure why this is happening.

+5
source share
1 answer

The after :create callback should really update the post object, however you call save! to your page object.

Try calling model.post.save! to save the user_id column in post directly.

+2
source

Source: https://habr.com/ru/post/1247998/


All Articles