変数モック方法:私のテストではインスタンス私はこのようなクラスのテストを記述しようとしています
from public_library import SuperClass
from another_library import Foo
class MyClass(SuperClass):
def __init__(self):
# Call super class
super(MyClass, self).__init__()
self._foo = Foo(
name='Some Name',
thing='Some Thing',
# self.something is defined in SuperClass.__init__()
cool=self.something.cool(),
)
を、私はFoo
クラスを嘲笑し、適切に設定されてself._foo
を確認するassert_called_once_with()
を呼び出しています。しかし、返り値self.something.cool()
を取得する方法を理解できません。
確かに戻り値をself
のインスタンス変数に設定し、それを私のテストで使用することはできますが、これは醜い解決策のようです。これを行うにはpatch()
の方法がありますか?
モック[それはと呼ばれていたものを記録](https://docs.python.org/3/library/unittest .mock.html#unittest.mock.Mock.mock_calls)。 – user2357112
[こちら](https://docs.python.org/3/library/unittest.mock.html#unittest.mock.Mock.call_args)もご覧ください。 – user2357112
@ user2357112: 'mock_calls'はかなり役に立ちますが、' self.something'を 'MagicMock'オブジェクトにどのようにパッチするのか分かりません。 –