Testing action with a .rate_limit

Hello,

I’m recently looking on how to test .rate_limit in my application, but I don’t really know how to be able to change the cache_store for only the test I’m running, since setting the store for all the application on test may trigger error while doing my other tests.

I’ve done a small app to test, but still don’t know how to specify a cache_store for on test.

I found that the cache store is passed into a lambda when the controller class is loaded. So the only option I see is to override the methods of the used store. This works for me:

cache = ActiveSupport::Cache::MemoryStore.new allow(Rails.cache).to receive(:increment, &cache.method(:increment)) 

This applies only for the current test example. It could affect other code using cache.increment but hopefully that’s not a problem.

1 Like