I've recently run into an issue that I'm surprised to have not seen before. I have classes like:
class Foo < ActiveRecord::Base
belongs_to :bar, inverse_of: :foos
end
class Bar < ActiveRecord::Base
has_many :foos, inverse_of: :bar
end
and I'm using fabricators like so (I know these look pointless):
Fabricator(:foo, class_name: Foo) do
bar fabricator: :bar
end
Fabricator(:bar, class_name: Bar) do
end
and in a test (RSpec) I'm doing this stub:
foo = Fabricate(:foo) # I can confirm that both foo and foo.bar are correct here.
Foo.stub(:find_by_id).and_return(foo)
My issue is in the test, when Foo.find_by_id
is called, it returns foo
correctly but foo.bar
is an RSpec mock, and saving foo
results in this error message:
Mock received unexpected message :marked_for_destruction? with (no args)
How can I ensure that the association is passed through with the stub as well? I found this thread but can't quite decipher what it all means.
Any help would be much appreciated!