我真的很喜欢mock 的标记值。在您只想编写覆盖一行的最小单元测试的情况下,不使用随机无意义数字是一种好方法。
但是,下面的
from mock import sentinel, patch
def test_multiply_stuff():
with patch('module.data_source1',return_value=sentinel.source1):
with patch('module.data_source1',return_value=sentinel.source1):
assert function(module.data_source1,
module_data2) == sentinel.source1 * sentinel.source2
不起作用。你会得到
TypeError: unsupported operand type(s) for *: '_SentinelObject' and '_SentinelObject'
我理解原因:哨兵对象上的操作不能计算为表达式是有道理的。
是否有某种技术可以做到这一点(最好在 mock 中)?
我可以使用一些 hack 吗?或者你能做的最好的事情就是使用模范数字?
请您参考如下方法:
也许最简单的方法是使用 id(sentinel_object) 而不是哨兵本身:
from mock import sentinel, patch
def test_multiply_stuff():
with patch('module.data_source1',return_value=sentinel.source1):
with patch('module.data_source2',return_value=sentinel.source2):
assert function(id(module.data_source1), id(module.data_source2) == id(sentinel.source1) * id(sentinel.source2)
