import ims.repository.invoice_repository as invoiceRepository
logger = logging.getLogger(__name__)
for invoice in invoices:
dbInvoice = invoiceRepository.get(invoice.invoice_id)
print("dbInvoice is %s" % dbInvoice) #prints <MagicMock etc.>
if dbInvoice is None:
logger.error("Unable to update status for invoice %d" % invoice.invoice_id)
from unittest import TestCase, mock
import ims.business.invoice_business as business
def test_invoiceDoesNotExist_logsErrorAndContinues(self, invoiceRepoMock, loggerMock):
invoice = Invoice(123)
loggerMock.error.assert_called_once_with("Unable to update status for invoice 123")
AssertionError: Expected 'get' to be called once. Called 0 times.
dbInvoice is <MagicMock name='invoiceRepository.get()' id='xxxx'>
invoiceRepoMock.get.return_value = None
The mock arguments to your test function are swapped. The inner decorator (for the logger) is applied first, so the mock logger should be the first argument to your method.
@mock.patch("ims.business.invoice_business.invoiceRepository") @mock.patch("ims.business.invoice_business.logger") def test_invoiceDoesNotExist_logsErrorAndContinues(self, loggerMock, invoiceRepoMock): ...