Search code examples
ruby-on-railsunit-testingmocha.jscounter-cache

How to test counter_cache with test::unit and mocha


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.


Solution

  • 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)