dimanche 23 juin 2019

Django testing - monkeypatching the same method on different objects

I have two objects which call the same method. However, I would like each of them to return different data.

Assuming a view which could contain something like:

def my_view(request):
    # Somehow fetching obj1 and obj2 using the ORM
    # ...
    data1 = obj1.get_data()
    data2 = obj2.get_data()
    return render(...)

Here would be the outline of a test:

@patch('model.get_data')
def test_returns_some_data(self, mock_get_data):
    mock_get_data.return_value = {'foo': 'bar'}

    resp = self.client.get(...)

In this example, both obj1 and obj2 would return the dictionary {'foo': 'bar'}.

What can I do in my test to make one of them return something else?

Thanks!

Aucun commentaire:

Enregistrer un commentaire