Beispiel #1
0
def test_nothing_returned_fails():
    """Raises an error if no message was received"""
    fake_client = Mock(spec=MQTTClient, message_received=Mock(return_value=None))

    expected = {"topic": "/a/b/c", "payload": "hello"}

    verifier = MQTTResponse(fake_client, "Test stage", expected, {})

    with pytest.raises(exceptions.TestFailError):
        verifier.verify(expected)

    assert not verifier.received_messages
    def _get_fake_verifier(self, expected, fake_messages, includes):
        """Given a list of messages, return a mocked version of the MQTT
        response verifier which will take messages off the front of this list as
        if they were published

        This mocks it as if all messages were returned in order, which they
        might not have been...?
        """
        if not isinstance(fake_messages, list):
            pytest.fail("Need to pass a list of messages")

        def yield_all_messages():
            msg_copy = fake_messages[:]

            def inner(timeout):
                try:
                    return msg_copy.pop(0)
                except IndexError:
                    return None

            return inner

        fake_client = Mock(spec=MQTTClient, message_received=yield_all_messages())

        return MQTTResponse(fake_client, "Test stage", expected, includes)