我当前正在尝试测试(pytest
)我的Django信号。我总是在这里收到此错误:
E AssertionError: Expected call: mock(sender=<ANY>, signal=<django.dispatch.dispatcher.Signal object at 0x10a29f668>)
E Actual call: mock(charge={[...]]}, order=Order: #weGnyRDPOL, sender=<class 'type'>, signal=<django.dispatch.dispatcher.Signal object at 0x10a29f668>)
我的信号包含打印语句:
@receiver(signal=charge_succeeded)
def create_influencer_transaction(sender, order, charge, **kwargs):
print("TEST")
显示执行pytest
TEST时,但是上面的错误停止了我的测试。你知道我的考试怎么了吗?
@contextmanager
def catch_signal(signal):
"""Catch django signal and return the mocked call."""
handler = mock.Mock()
signal.connect(handler)
yield handler
signal.disconnect(handler)
@pytest.fixture
def tester():
return StripeTester()
@pytest.mark.django_db
def test_should_send_signal_when_charge_succeeds(client, monkeypatch, tester):
with catch_signal(charge_succeeded) as handler:
# Charge normally are several lines of code that also include
# client, monkeypatch, tester. However, that part works as I
# receive the signal (see below). That's why I simplified it.
charge(100)
handler.assert_called_once_with(
sender=mock.ANY,
signal=charge_succeeded,
)