Documentation
¶
Overview ¶
Package testing provides test helpers for the cache package.
Usage:
func TestMyFeature(t *testing.T) {
// Create a fake Redis store for testing
store, cleanup := testing.FakeRedis(t, "myapp")
defer cleanup()
// Use the store in your tests
store.Put("key", "value", time.Hour)
// ...
}
Index ¶
- func FakeManager(t testing.TB, prefix string) (*cache.Manager, func())
- func FakeManagerMemory(t testing.TB, prefix string) (*cache.Manager, func())
- func FakeMemory(t testing.TB, prefix string) (*drivers.MemoryStore, func())
- func FakeRedis(t testing.TB, prefix string) (*drivers.RedisStore, func())
- func FakeRedisWithServer(t testing.TB, prefix string) (*drivers.RedisStore, *miniredis.Miniredis, func())
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FakeManager ¶
FakeManager creates a cache manager configured with a fake Redis store. Useful for testing code that uses the cache.Manager interface.
Example:
func TestWithManager(t *testing.T) {
manager, cleanup := testing.FakeManager(t, "app")
defer cleanup()
manager.Put("key", "value", time.Hour)
// ...
}
func FakeManagerMemory ¶
FakeManagerMemory creates a cache manager configured with an in-memory store. Faster than FakeManager since it doesn't need miniredis.
Example:
func TestWithMemoryManager(t *testing.T) {
manager, cleanup := testing.FakeManagerMemory(t, "app")
defer cleanup()
manager.Put("key", "value", time.Hour)
// ...
}
func FakeMemory ¶
func FakeMemory(t testing.TB, prefix string) (*drivers.MemoryStore, func())
FakeMemory creates an in-memory cache store for testing. It returns the store and a cleanup function that should be called with defer.
Example:
func TestCache(t *testing.T) {
store, cleanup := testing.FakeMemory(t, "app")
defer cleanup()
store.Put("key", "value", time.Hour)
// ...
}
func FakeRedis ¶
func FakeRedis(t testing.TB, prefix string) (*drivers.RedisStore, func())
FakeRedis creates a fake Redis store backed by miniredis for testing. It returns the store and a cleanup function that should be called with defer.
Example:
func TestCache(t *testing.T) {
store, cleanup := testing.FakeRedis(t, "app")
defer cleanup()
store.Put("user:1", map[string]string{"name": "John"}, time.Hour)
value, found := store.Get("user:1")
// ...
}
func FakeRedisWithServer ¶
func FakeRedisWithServer(t testing.TB, prefix string) (*drivers.RedisStore, *miniredis.Miniredis, func())
FakeRedisWithServer creates a fake Redis store and also returns the miniredis server for advanced testing scenarios (e.g., simulating time, network errors).
Example:
func TestCacheExpiration(t *testing.T) {
store, server, cleanup := testing.FakeRedisWithServer(t, "app")
defer cleanup()
store.Put("key", "value", 100*time.Millisecond)
server.FastForward(200 * time.Millisecond)
_, found := store.Get("key")
if found {
t.Error("expected key to be expired")
}
}
Types ¶
This section is empty.