Mocking out two redis hgets with different return values in the same python function
<p>I have some code like this:</p>

<pre><code>import redis

redis_db = redis.Redis(host=redis_host_ip, port=redis_port, password=redis_auth_password)

def mygroovyfunction():
    var_a = redis_db.hget(user, 'something_a')
    var_b = redis_db.hget(user, 'something_b')
    if var_a == something_a:
        return Response(json.dumps({}), status=200, mimetype='application/json')
    if var_b == something_b:
        return Response(json.dumps({}), status=400, mimetype='application/json')
</code></pre>

<p>And then in a tests.py file for unit testing this, I have some code like this:</p>

<pre><code>import unittest
from mock import MagicMock, Mock, patch

@patch('redis.StrictRedis.hget', return_value='some value')
class MyGroovyFunctionTests(unittest.TestCase):
    def test_success(self, mock_redis_hget):
        response = self.app.get('mygroovyfunction')
        self.assertEqual(response.status_code, 200)
</code></pre>

<p>So there's some other flask stuff which I left out because it is not relevant for this question. </p>

<p>What I wanted to know was if it is possible to mock a return value for each individual redis hget. With my current code, the mocked return value replaces <code>var_b</code>, and so when the test case runs, it makes <code>var_a</code> also the same value, causing the code to go down the path ending in a return status_code of 400.</p>

<p>What is the proper way to do this kind of thing?</p>

<p>Ok I found the answer here: <a href="http://stackoverflow.com/questions/24897145/python-mock-multiple-return-values">Python mock multiple return values</a></p>

<p>The accepted answer for that question is what I was looking for, which is to use <code>side_effect</code> and make it a list of values and so each patched redis hget will be given each value in the list. So for my example the solution is to do this:</p>

<pre><code>import unittest
from mock import MagicMock, Mock, patch

@patch('redis.StrictRedis.hget', side_effect=['something_a','something_b'])
class MyGroovyFunctionTests(unittest.TestCase):
    def test_success(self, mock_redis_hget):
        response = self.app.get('mygroovyfunction')
        self.assertEqual(response.status_code, 200)
</code></pre>

<p>Thanks <a href="http://stackoverflow.com/users/100297/martijn-pieters">http://stackoverflow.com/users/100297/martijn-pieters</a> for the answer I was looking for :)</p>

python unit-testing mocking python-unittest python-mock