Using activerecord 4.2.7.1 and factory_girl 4.8.0. I upgraded from a previous version of activerecord and factory_girl and a number of our specs started failing.
When passing an instance created using the stub strategy as a relation to an instance created with the create strategy, and saving the persisted instance, factory girl raises an error on the #destroyed? method.
e.g.:
foo = build_stubbed(:foo)
bar = create(:bar, foo: foo)
bar.save!
This breaks the spec.
#destroyed? is called in ActiveRecord::AutosaveAssociation#save_belongs_to_association. I'm fine with the notion of preventing access to the database in stubbed models, but I don't think #destroyed? actually calls the database.
Note that it has a similar implementation to #persisted? which is actually stubbed out in FactoryGirl::Strategy::Stub. It calls ActiveRecord::Base#sync_with_transaction_state and then returns a value. #sync_with_transaction_state does not appear to actually access the database in any way.
I'm wondering if I'm either 1) offbase, or 2) just shouldn't be doing what I'm doing in the first place. If not, would it be possible to remove #destroyed? from the list of disabled persistence methods?
Just came here with the same info, it has caused a large number of our specs to fail as well.
I can understand from a purity point of view that stubbing one record in an object graph seems a little strange, but it doesn't seem like the change was made to specifically address that issue.
I've created a PR https://github.com/thoughtbot/factory_girl/pull/982
I believe this has been fixed in https://github.com/thoughtbot/factory_bot/commit/168bbf8e346487973d2b50496ed2d93100f83276 and released from 4.8.1 onwards.
Please feel free to reopen if this is issue hasn't been resolved.
Most helpful comment
Just came here with the same info, it has caused a large number of our specs to fail as well.
I can understand from a purity point of view that stubbing one record in an object graph seems a little strange, but it doesn't seem like the change was made to specifically address that issue.
I've created a PR https://github.com/thoughtbot/factory_girl/pull/982