I have some code in a Rails model that does this:
has_one :plan
validates_presence_of :plan
And used to do this:
after_initialize :set_plan
def set_plan
self.plan ||= FreePlan.new
end
and now does:
def plan
super || (self.plan = FreePlan.new)
end
Now, however, this test fails:
it { is_expected.to validate_presence_of(:plan) }
The new code is nicer in that it doesn't always have to look up a plan object in the DB for every instantiation of this object, but I'm curious what the test is doing in terms of the object under test and its lifecycle.