How to test counter_cache with test::unit and mocha
-
15-10-2019 - |
문제
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.
제휴하지 않습니다 StackOverflow