سؤال

I'm curious if i can mock out somehow the testing of a counter cache. Now I'm creating all the records to test the counter_cache. Is there any good practices to test counter cache effectively?

Edit: I'm just interested in testing that, the counter cache exists on a certain association.

هل كانت مفيدة؟

المحلول

When you add the :counter_cache option, ActiveRecord defines 2 callbacks (source code). You can use Mocha to check if the callback is executed.

instance.expects(:belongs_to_counter_cache_after_create_for_MODELNAME).returns(true)

نصائح أخرى

Add an entry, reload your object with counter_cache and check if it's increment.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top