0
親クラスで使用されているメソッドを持つ別のクラスのインスタンスであるインスタンス変数を正しくモックする方法を理解しようとしています。ここでPythonのインスタンス変数のモックメソッド
は、問題領域の簡単な例です:
import unittest
import mock
class Client:
def action(self):
return True
class Service:
def __init__(self):
self.client = Client()
class Handler:
def __init__(self):
self._service = Service()
def example(self):
return self._service.client.action()
class TestHandler(unittest.TestCase):
@mock.patch('__main__.Handler._service')
def test_example_client_action_false(self):
"""Test Example When Action is False"""
handler = Handler()
self.assertFalse(handler.example())
if __name__ == '__main__':
unittest.main()
結果のテストが発生します
AttributeError: __main__.Handler does not have the attribute '_service'
どのように私はきちんと私のテストケースのためService
またはClient
ようaction
戻りFalse
を模擬ん?