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

1

7

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.

Beetroot answered 27/4, 2016 at 0:5 Comment(0)
C
4

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.

Cystolith answered 27/4, 2016 at 0:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.