@@ -4,105 +4,71 @@ This guide explains how to effectively test applications that use ElixirCache, f
44
55## Using the Sandbox Mode
66
7- ElixirCache provides a sandbox mode specifically designed for testing. This ensures that your tests:
7+ ElixirCache provides a sandbox mode that gives each test its own isolated cache
8+ namespace. This ensures your tests:
89
9101 . Are isolated from each other
10112 . Don't leave lingering cache data between test runs
11123 . Can run in parallel without conflicts
1213
13- ### Configuring Your Cache for Testing
14+ ### Configuring Your Cache
1415
15- In your test environment, you can wrap any cache adapter with the sandbox functionality:
16+ Use ` sandbox?: Mix.env() === :test ` on your cache module. The adapter stays the
17+ same in every environment — the sandbox wraps whatever adapter you choose:
1618
1719``` elixir
18- # In lib/my_app/cache.ex
1920defmodule MyApp .Cache do
2021 use Cache ,
21- adapter: get_cache_adapter () ,
22+ adapter: Cache . Redis ,
2223 name: :my_app_cache ,
23- opts: get_cache_opts (),
24- # Enable sandbox mode in test environment
25- sandbox?: Mix .env () == :test
26-
27- defp get_cache_adapter do
28- case Mix .env () do
29- :test -> Cache .ETS
30- :dev -> Cache .ETS
31- :prod -> Cache .Redis
32- end
33- end
34-
35- defp get_cache_opts do
36- case Mix .env () do
37- :test -> []
38- :dev -> []
39- :prod -> [host: " redis.example.com" , port: 6379 ]
40- end
41- end
24+ opts: [uri: " redis://localhost:6379" ],
25+ sandbox?: Mix .env () === :test
4226end
4327```
4428
29+ When ` sandbox? ` is ` true ` , ` Cache.Sandbox ` is used as the adapter automatically.
30+ You do not need to switch adapters between environments.
31+
4532### Setting Up the Sandbox Registry
4633
47- To use the sandbox functionality in your tests, you need to start the ` Cache.SandboxRegistry ` in your test setup :
34+ Add ` Cache.SandboxRegistry.start_link() ` to your ` test/test_helper.exs ` :
4835
4936``` elixir
5037# In test/test_helper.exs
38+ Cache .SandboxRegistry .start_link ()
5139ExUnit .start ()
52-
53- # Start the sandbox registry for your tests
54- {:ok , _pid } = Cache .SandboxRegistry .start_link ()
55-
56- # Start your application's supervision tree
57- Application .ensure_all_started (:my_app )
5840```
5941
6042### Using the Sandbox in Tests
6143
62- Using the sandbox in your tests is very simple. All you need to do is start the sandbox registry in your setup block:
44+ Register your cache in each test's ` setup ` block with
45+ ` Cache.SandboxRegistry.start/1 ` . This starts the cache supervisor and
46+ registers the current test process for isolation:
6347
6448``` elixir
6549defmodule MyApp .CacheTest do
6650 use ExUnit .Case , async: true
6751
68- defmodule TestCache do
69- use Cache ,
70- adapter: Cache .Redis , # The actual adapter doesn't matter in sandbox mode
71- name: :test_cache ,
72- opts: [],
73- sandbox?: Mix .env () === :test
74- end
75-
7652 setup do
77- # This single line is all you need to set up sandbox isolation
78- Cache .SandboxRegistry .start (TestCache )
53+ Cache .SandboxRegistry .start (MyApp .Cache )
7954 :ok
8055 end
81-
82- test " can store and retrieve values" do
83- assert :ok = TestCache .put (" test-key" , " test-value" )
84- assert {:ok , " test-value" } = TestCache .get (" test-key" )
85- end
86-
87- test " can handle complex data structures" do
88- data = %{users: [%{name: " Alice" }, %{name: " Bob" }]}
89- assert :ok = TestCache .put (" complex-key" , data)
90- assert {:ok , ^data } = TestCache .get (" complex-key" )
56+
57+ test " stores and retrieves values" do
58+ assert :ok === MyApp .Cache .put (" key" , " value" )
59+ assert {:ok , " value" } === MyApp .Cache .get (" key" )
9160 end
9261
93- test " provides isolation between tests" do
94- # This will return nil because each test has an isolated cache
95- assert {:ok , nil } = TestCache .get (" test-key" )
62+ test " each test is isolated" do
63+ assert {:ok , nil } === MyApp .Cache .get (" key" )
9664 end
9765end
9866```
9967
100-
10168## Tips for Testing with ElixirCache
10269
103- 1 . ** Always use the sandbox in tests** : This prevents interference between tests.
104- 2 . ** Clean up after each test** : Use ` on_exit ` to unregister from the sandbox.
105- 3 . ** Use unique keys** : Even with sandboxing, using descriptive, unique keys makes debugging easier.
106- 4 . ** Test edge cases** : Including cache misses, errors, and TTL expiration.
107- 5 . ** Consider using fixtures** : For commonly cached data structures.
108- 6 . ** Verify telemetry events** : If your application relies on cache metrics.
70+ 1 . ** Always use ` sandbox?: Mix.env() === :test ` ** : Keep the same adapter everywhere — the sandbox handles isolation.
71+ 2 . ** Use ` Cache.SandboxRegistry.start/1 ` in setup** : This is the only line needed per test.
72+ 3 . ** Tests can be ` async: true ` ** : Each test gets its own sandbox namespace.
73+ 4 . ** Test edge cases** : Cache misses, errors, and TTL expiration.
74+ 5 . ** Verify telemetry events** : If your application relies on cache metrics.
0 commit comments