When you're writing specs for ActiveRecord models that use
memoization
Show archive.org snapshot
, a simple #reload
will not do:
it 'updates on changes' do
subject.seat_counts = [5]
subject.seat_total.should == 5
# seat_total is either memoized itself, or using some
# private memoized method
subject.seat_counts = [5, 1]
subject.seat_total.reload.should == 6 # => Still 5
end
You might be tempted to manually unmemoize any memoized internal method to get #seat_total
to update, but that has two disadvantages:
record.unmemoize_all
Perform a full reload by re-finding your record:
it 'updates on changes' do
subject.seat_counts = [5]
subject.seat_total.should == 5
subject.seat_counts = [5, 1]
# Re-find subject to wipe memoization
subject = described_class.find(subject.id)
subject.seat_total.reload.should == 6 # Passes
end
Note that this does not work if you're testing unpersisted new records. In this case, there's probably something conceptually wrong and you either shouldn't expect to see updates or shouldn't memoize at all.