我想模擬一個屬性以依次回傳不同的值,就像在side_effectMagic Mock 上使用時一樣。現在我正在這樣做:
mock = <my magic mock here>
type(mock).information = PropertyMock()
type(mock).information.side_effect = [1,2]
mock.information # it does not return 1
mock.information # it does not return 2
知道如何正確模擬它嗎?
uj5u.com熱心網友回復:
from unittest.mock import MagicMock, PropertyMock
m = MagicMock()
type(m).foo = PropertyMock(side_effect=[1, 2])
print(m.foo) # 1
print(m.foo) # 2
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/389708.html
標籤:蟒蛇-3.x 单元测试 嘲笑 python-3.6
上一篇:如何測驗Laravel管道
下一篇:類中的模擬背景關系管理器
