オブジェクトをインスタンス化し、そのメソッドを呼び出すクラスがUUT
あります。オブジェクトは、次の 2 つ
の目的でオブジェクトを使用します。 Worker
do_stuff()
Worker
Provider
- プロバイダー オブジェクトのメソッドを呼び出して、何らかの処理を行います。
- プロバイダーのイベントでメソッドをサブスクライブすることにより、プロバイダーから通知を取得します
ワーカーが通知を受け取ると、それを処理し、オブジェクトに通知します。UUT
オブジェクトは、それに応じてさらにオブジェクトを作成できWorker
ます。
私はすでに各クラスを単独でテストしており、UUT
+Worker
を一緒にテストしたいと考えています。そのために、私はモックアウトするつもりProvider
です。
import mock
import unittest
import provider
class Worker():
def __init__(self, *args):
resource.default_resource.subscribe('on_spam', self._on_spam) # I'm going to patch 'resource.default_resource'
def do_stuff(self):
self.resource.do_stuff()
def _on_spam(self, message):
self._tell_uut_to_create_more_workers(message['num_of_new_workers_to_create'])
class UUT():
def __init__(self, *args):
self._workers = []
def gen_worker_and_do_stuff(self, *args)
worker = Worker(*args)
self._workers.append(resource)
worker.do_stuff()
class TestCase1(unittest.TestCase):
@mock.patch('resource.default_resource', spec_set=resource.Resource)
def test_1(self, mock_resource):
uut = UUT()
uut.gen_worker_and_do_stuff('Egg') # <-- say I automagically grabbed the resulting Worker into self.workers
self.workers[0]._on_spam({'num_of_new_workers_to_create':5}) # <-- I also want to get hold of the newly-created workers
(実装の詳細である) のリストにuut
直接アクセスせずに、によって生成されたワーカー オブジェクトを取得する方法はありますか?_workers
uut
Worker.__init__
ワーカーがプロバイダー イベントにサブスクライブする でそれを実行できると思います。そのため、質問は次の
ようになりself
ますresource.default_resource.subscribe('on_spam', self._on_spam)
。