@@ -97,85 +97,6 @@ defmodule MyApp.CacheTest do
9797end
9898```
9999
100- ## Testing with Multiple Cache Modules
101-
102- If your application uses multiple cache modules, register each one with the sandbox:
103-
104- ``` elixir
105- setup do
106- sandbox_id = " test-#{ :erlang .unique_integer ([:positive ])} "
107-
108- :ok = Cache .SandboxRegistry .register (MyApp .UserCache , sandbox_id)
109- :ok = Cache .SandboxRegistry .register (MyApp .SessionCache , sandbox_id)
110-
111- on_exit (fn ->
112- Cache .SandboxRegistry .unregister (MyApp .UserCache )
113- Cache .SandboxRegistry .unregister (MyApp .SessionCache )
114- end )
115-
116- :ok
117- end
118- ```
119-
120- ## Testing Asynchronously
121-
122- The sandbox functionality allows for safe asynchronous testing:
123-
124- ``` elixir
125- defmodule MyApp .AsyncCacheTest do
126- use ExUnit .Case , async: true
127-
128- setup do
129- sandbox_id = " test-#{ :erlang .unique_integer ([:positive ])} "
130- :ok = Cache .SandboxRegistry .register (MyApp .Cache , sandbox_id)
131-
132- on_exit (fn ->
133- Cache .SandboxRegistry .unregister (MyApp .Cache )
134- end )
135-
136- :ok
137- end
138-
139- # Tests run in parallel without interfering with each other
140- test " first test" do
141- assert :ok = MyApp .Cache .put (" key-1" , " value-1" )
142- assert {:ok , " value-1" } = MyApp .Cache .get (" key-1" )
143- assert {:ok , nil } = MyApp .Cache .get (" key-2" ) # Doesn't see other test's data
144- end
145-
146- test " second test" do
147- assert :ok = MyApp .Cache .put (" key-2" , " value-2" )
148- assert {:ok , " value-2" } = MyApp .Cache .get (" key-2" )
149- assert {:ok , nil } = MyApp .Cache .get (" key-1" ) # Doesn't see other test's data
150- end
151- end
152- ```
153-
154- ## Mocking Cache Interactions
155-
156- For unit tests where you want to mock the cache entirely:
157-
158- ``` elixir
159- defmodule MyApp .ServiceTest do
160- use ExUnit .Case
161- import Mox
162-
163- # Define a mock for your cache
164- defmock (MockCache , for: MyApp .CacheBehaviour )
165-
166- setup :verify_on_exit!
167-
168- test " service uses cache correctly" do
169- # Set up expectations for the mock
170- expect (MockCache , :get , fn " user:1" -> {:ok , %{name: " Test User" }} end )
171- expect (MockCache , :put , fn " user:1" , _ttl , _data -> :ok end )
172-
173- # Test your service that uses the cache
174- result = MyApp .Service .update_user (1 , %{name: " Updated User" })
175- assert result == :ok
176- end
177- end
178- ```
179100
180101## Tips for Testing with ElixirCache
181102
0 commit comments