Factory_bot: Factory Girl behaves differently in after_initialized than ActiveRecord

Created on 9 Aug 2012  路  1Comment  路  Source: thoughtbot/factory_bot

Something similar to this is failing for me:

class User < ActiveRecord::Base
after_initialize :do_something

def do_something
some_other_column = test
end
end

Tests

attrs = {:test => true}
fg_user = FactoryGirl.buid(User, attrs)
ar_user = User.new(attrs)

fg.some_other_column.should eq ar_user.some_other_column

If I use binding.pry in the after_initialize block, attributes are set when doing the AR new, but not FG build.

Most helpful comment

FactoryGirl assigns attributes after initialization intentionally to avoid attr_accessible issues. You can change how it initializes your class using initialize_with.

>All comments

FactoryGirl assigns attributes after initialization intentionally to avoid attr_accessible issues. You can change how it initializes your class using initialize_with.

Was this page helpful?
0 / 5 - 0 ratings