Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Created July 6, 2015 12:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igniteflow/4e97eb41e2b9a80cd5de to your computer and use it in GitHub Desktop.
Save igniteflow/4e97eb41e2b9a80cd5de to your computer and use it in GitHub Desktop.
Mock Django's cache in unit tests
"""
myapp/mymodule.py:
from django.core.cache import cache
def set_cache():
cache.set('foo', 'bar')
def get_cache():
return cache.get('foo')
"""
from myapp.mymodule import set_cache, get_cache
with patch('myapp.mymodule.cache') as mock_cache:
cache = {}
def get(key, default=None):
return cache.get(key, default)
def _set(key, value, timeout=60):
cache[key] = value
mock_cache.get = get
mock_cache.set = _set
set_cache()
self.assertEqual(cache['foo'], 'bar')
self.assertEqual(get_cache(), 'bar')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment