`MagicMock` 可以与 python3 中的 int 进行比较吗?

Joh*_*ith 5 python python-2.7 python-3.x

我正在迁移我的项目的 python 版本 (2->3)。测试对 python2 工作正常,但对 python3 抱怨,错误就像

TypeError: '>' not supported between instances of 'MagicMock' and 'int'
Run Code Online (Sandbox Code Playgroud)

这是一个最小的案例

TypeError: '>' not supported between instances of 'MagicMock' and 'int'
Run Code Online (Sandbox Code Playgroud)

赶紧跑 py.test .

这些黑客不起作用

MagicMock.__le__ = some_le_method # just not working

MagicMock.__le__.__func__.__code = some_le_method.__func__.__code__ # wrapper_descriptor does not have attribute __func__
Run Code Online (Sandbox Code Playgroud)

小智 1

您应该分配__gt__内部ba.value

# self is MagicMock itself
b.__gt__ = lambda self, compare: True
# or
a.value.__gt__ = lambda self, compare: True
Run Code Online (Sandbox Code Playgroud)