Repository navigation
Make null_store the default cache store in test environment config - #33773
Conversation
|
Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @eileencodes (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review. Please see the contribution instructions for more information. |
|
I’ve stubbed my toe on this before. 👍 Nothing changes for existing apps unless they opt into the new default via |
|
@mcnelson can you open a new PR with a changelog? It's important to mention this change in behavior. |
|
Done: #33779 |
|
This default means
|
|
@Kerrick Would you mind sending a PR? |
|
I recently had a hard time testing rate limits on controllers. Ideally, I think the best would be to be able to swap caching backend on-demand. That is, rather than globally switch to It seems to me that FWIW, I've come up with a custom solution: # config/environments/test.rb
require "cache/store_proxy"
Rails.application.configure do
# ...
config.cache_store = StoreProxy.new
# ...
end
# lib/cache/store_proxy.rb
class StoreProxy
def initialize(...)
@store = ActiveSupport::Cache::NullStore.new(...)
end
def self.supports_cache_versioning?
true
end
def with_memory_store(...)
was_store = @store
@store = ActiveSupport::Cache::MemoryStore.new(...)
yield
ensure
@store = was_store
end
def method_missing(name, ...)
@store.send(name, ...)
end
endThen, in a test: test "create is rate limited" do
params = { email_address: "foo@example.com", password: "wrong" }
Rails.cache.with_memory_store do
10.times do
post(session_path, params:)
assert_response :redirect
end
post(session_path, params:)
assert_response 429
end
endNothing too hard to implement, really, but I think it would be nice if Rails would provide a more fine-grained solution for controlling caching in rate limit tests. Let me know if you want to talk about this further. |
Summary
It's surprising to me that by default, low-level caching is functional in the test environment. This simply adds
config.cache_store = :null_storeinconfig/environments/test.rb.The existing defaults in
test.rbas well as the Rails Guides kinda have you believe thenull_storeis the default.Really was on the fence about opening this as it may not be a substantial enough change to warrant dev resources. But since Rails has such a great reputation of providing sane defaults, I felt like this would be worth it.
Other Information
I perused the tests and wasn't sure where best to add anything.
railties/test/application/generators_test.rb?Appropriate to update CHANGELOG?