[Answered ]-Django: mock post_save signal handler?

2👍

FWIW I followed the same, but needed to mock celery’s send_task. After reading through, I did recognize, that signals are valuable and shouldn’t be mocked (it’s a desired action to fire them, right?), so the solution was to mock what was going on inside the signal (communication with external services). All in all I would suggest:

from unittest.mock import patch
from orders.models import Order

class OrderModelTest(CartSetupTestCase):

    @patch('orders.signals.SlackNotification.objects.create')
    def test_string_representation(self, create):
        order = Order.objects.create(
            user=self.user,
            merchant_uid="1475633246629",
            customer_name="asd",
            address="주소",
            address_detail="asdfdsa",
            postal_code="12345",
            phone_number="01095104344",
            possible_date_start="2011-11-24",
            possible_date_end="2011-11-24",
            possible_time_start="11:22 AM",
            possible_time_end="11:22 AM",
            total_price=self.cart.total_price,
        )
        self.assertEquals(1, create.call_count)
👤brian

Leave a comment