I have the following function:
import unittest from unittest import mock def get_payments(order): return order.payments.filter(status='complete').order_by('-date_added)
I want to make fun of the filter and order_by methods to check the arguments that are being called.
I tried:
class TestPayments(unittest.TestCase): @mock.patch('path.Order.payments.filter.order_by') @mock.patch('path.Order.payments.filter') def test_get_payments(self, mock1, mock2): mock1.assert_called_with(status='complete') mock2.assert_called_with('-date_added')
Another layout I tried:
@mock.patch('path.Payment.objects.filter.order_by') @mock.patch('path.Payment.objects.filter') @mock.patch('path.Order.payments.objects.filter.order_by') @mock.patch('path.Order.payments.objects.filter')
In the last two mocks, I have an error that path.Order does not exist. I have already used the direct layout for a request of type Payment.objects.filter() and it works, but starting with a sister model of type Order I have failed.
The connection between Order and Payment is, as you expect, from one to many.
source share