Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rspec/FactoryGirl: changes in factory not saving in test database?

I have the following factory defined for a 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

the after create block seems to run file and the factory returns the model object with the correct/new changes however this does not persist in my test database.

ie. if i call Page.last.post.user.id from within my test, i still get the old user id which was assigned to the post object factory earlier before the after create block. I'm not sure why this happens.

like image 663
L457 Avatar asked Apr 27 '16 00:04

L457


1 Answers

Your after :create callback should really be updating the post object, however, you're calling save! on your page object.

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

like image 166
Anthony E Avatar answered Sep 25 '22 23:09

Anthony E