私は実際のrabbitmqサービスを実行することなくテストしたいと思っているkombu.ConsumerProducerMixin
から継承するクラスを持っています。pytestで接続クラスを模擬してください
from unittest.mock import Mock, patch
from aggregator import Aggregator
@patch('kombu.connection.Connection')
def test_on_request(conn_mock):
agg = Aggregator('localhost')
m = Message("", {"action": "start"}, content_type="application/json")
デバッガでAggregator.__init__
にステッピング、私はconnection
はまだMock
インスタンスであることをパッチが適用されていないことを参照してください:
(Pdb) self.connection
<Connection: amqp://guest:**@localhost:5672// at 0x7fc8b7f636d8>
(Pdb) Connection
<class 'kombu.connection.Connection'>
私のテストファイルで
class Aggregator(ConsumerProducerMixin):
def __init__(self, broker_url):
exchange_name = 'chargers'
self.status = 0
self.connection = Connection(broker_url)
...
は、私は次のようでした
私の質問は、私が正しくテストするためにrabbitmqを必要としないように接続を正しくパッチするかどうかです。