skip to content

Why does a rate_limit never trip in a generated Rails 8.1 test suite, and how do you test the limit anyway?

level: middleimportance: should knowfreq 30%

answer

  1. test env uses :null_store
  2. NullStore#increment returns nil
  3. nil count never exceeds to:
  4. store captured at declaration
  5. assert_response :too_many_requests

basics

~20 s

The generated test environment sets config.cache_store = :null_store, whose increment returns nil, so the count never exceeds to:; test it by giving the limit a real store, such as a memory store in test, and clearing it between tests.

solid answer

~40 s

`rate_limit` counts with `store.increment`, and the default store is the controller's cache store. The generated `config/environments/test.rb` sets `config.cache_store = :null_store`, and `ActiveSupport::Cache::NullStore#increment` returns `nil`; the filter only refuses when `count && count > to`, so every request passes. To test the limit, the counter needs a real store: switch the test environment to `:memory_store` and call `Rails.cache.clear` in `setup`, or declare the limit with an explicit `store:` constant that the test environment backs with `ActiveSupport::Cache::MemoryStore.new`. Changing `cache_store` inside a single test is too late, because the default store is captured when the `rate_limit` line runs at class load. Then send `to + 1` requests and assert `:too_many_requests`; the test environment's `show_exceptions = :rescuable` turns the raised error into the 429 response.

code

ruby · 14 lines
ruby
# config/environments/test.rb
config.cache_store = :memory_store

# test/integration/sign_in_rate_limit_test.rb
class SignInRateLimitTest < ActionDispatch::IntegrationTest
  setup { Rails.cache.clear }

  test "eleventh sign-in attempt in three minutes is refused" do
    10.times { post session_path, params: { email_address: "[email protected]", password: "wrong" } }
    post session_path, params: { email_address: "[email protected]", password: "wrong" }
    assert_redirected_to new_session_path
    assert_equal "Try again later.", flash[:alert]
  end
end

go deeper

for a junior

Know that the generated test environment uses a null cache store, so rate limits never trigger there unless you change it.

for a middle

Explain NullStore#increment returning nil, the count && guard, and the store being captured when the macro runs, then write a test with a memory store.

for a senior

Isolate rate-limit storage from general caching, clear it between tests, test expiry with travel, and make sure security-relevant limits have real coverage.

for a principal

Treat untested protections as unverified: require behavioural tests for every abuse control, and design them so test configuration cannot silently disable them.

## Why the limit is invisible in tests A new Rails 8.1 app's `config/environments/test.rb` contains: ```ruby config.cache_store = :null_store ``` That keeps caching from leaking state between tests. `rate_limit` does not care whether caching is "on"; it calls the store directly: ```ruby count = store.increment(cache_key, 1, expires_in: within) if count && count > to # run with: end ``` `ActiveSupport::Cache::NullStore#increment` has an empty body, so it returns `nil`. The `count &&` guard then short-circuits, and no request is ever refused. A test that posts 11 times to the ride-hailing app's sign-in form and expects a refusal will fail, while the limit works in production. ## The store is chosen at class load The macro's signature is `rate_limit(to:, within:, by: ..., with: ..., store: cache_store, ...)`. Ruby evaluates the `store:` default when `rate_limit` runs in the class body, so the limit keeps a reference to whatever `cache_store` returned at that moment. Two consequences: 1. Setting a different cache store in one test's `setup` does not affect limits that are already declared. 2. Configuration that runs before the app loads, such as `config/environments/test.rb`, does take effect. ## Three ways to make it testable | Approach | How | Trade-off | |---|---|---| | Memory store for the whole test env | `config.cache_store = :memory_store` in `test.rb`, `Rails.cache.clear` in `setup` | other caching code now caches in tests too | | Dedicated limit store | declare `store: RATE_LIMIT_STORE`, set it to `ActiveSupport::Cache::MemoryStore.new` in test | limits stay isolated from page caching | | Unit-test the `with:` method | call the method directly | proves the response, not the counting | Whichever you choose, clear the store between tests. Counters keyed by `127.0.0.1`, the integration test's remote address, otherwise leak from one test into the next. ## Writing the test 1. Arrange a real store, as above. 2. Send `to` requests and assert they pass. 3. Send one more and assert the refusal. 4. Use `travel` to move past `within` and assert the next request passes again; Active Support's time helpers move `Time.now`, which the memory store uses for expiry. In the generated test environment `config.action_dispatch.show_exceptions = :rescuable`. `ActionController::TooManyRequests` is a mapped, rescuable exception, so an integration test receives a 429 response rather than a raised error, and `assert_response :too_many_requests` works. If the limit has a custom `with:`, such as the generator's redirect with "Try again later.", assert that instead. ## A dedicated store, end to end Isolating limits from general caching also makes them easy to test: ```ruby # config/initializers/rate_limit_store.rb RATE_LIMIT_STORE = Rails.env.test? ? ActiveSupport::Cache::MemoryStore.new : Rails.cache # app/controllers/quotes_controller.rb rate_limit to: 30, within: 1.minute, only: :create, store: RATE_LIMIT_STORE ``` Tests call `RATE_LIMIT_STORE.clear` in `setup`, while page caching in tests stays on the null store. In production the constant is the shared cache, so behaviour is unchanged. ## What not to do - Do not assert on the cache key string: it is an implementation detail; assert on behaviour. - Do not leave the test environment on `:null_store` and conclude the limit works because the action succeeded. - Do not raise `to:` in tests to make slow tests pass; use a dedicated small store and clear it. The generated sign-in and password-reset controllers each carry a `rate_limit`, so this is the first place a team notices the problem: their tests pass whether or not the limit exists.

  • Why does setting ApplicationController.cache_store = :memory_store in a test's setup not help?
    The `store:` default is evaluated when the `rate_limit` line runs at class load, so each declared limit already holds the null store object. Changing the controller's cache store afterwards affects caching, not those limits. Configure the store before the app loads, or pass an explicit `store:` you control.
  • Do counters leak between integration tests once a memory store is used?
    Yes. Every integration request comes from the same remote address, so keys like `rate-limit:sessions:127.0.0.1` persist across tests in the same process. Clear the store in `setup`, or the order tests run in starts deciding which of them get refused.

A null store is a turnstile counter with no display: everyone walks through, and the attendant who checks 'more than ten?' never sees a number, so never stops anyone.

  • The test passes, so the rate limit must be working
  • Turning on controller caching makes rate_limit count in tests
  • Changing the cache store in setup applies to declared limits
  • NullStore#increment returns zero, so requests are refused
  • TooManyRequests raises out of integration tests by default