问题描述
这是回溯
Q1.为什么会抛出这个错误?它们是 <class type='MagicMock>
Q2.如果错误得到修复,如何暂停模拟以便第一行通过?
来自 文档:p><块引用>
通常,对象的 __class__
属性将返回其类型.对于具有规范的模拟对象,__class__
改为返回规范类.这允许模拟对象通过 isinstance()
测试它们正在替换/伪装为的对象:
不要使用 isinstance
,而是检查 say_it
方法是否存在.如果方法存在,调用它:
无论如何,这是一个更好的设计:依赖类型信息更加脆弱.
Here is the traceback
Q1. Why is this error thrown? They are <class type='MagicMock>
Q2. How do I pause the mocking so that the first line will pass if the error is fixed?
From the docs:
Normally the
__class__
attribute of an object will return its type. For a mock object with a spec,__class__
returns the spec class instead. This allows mock objects to passisinstance()
tests for the object they are replacing / masquerading as:
Don't use isinstance
, instead check for the existence of the say_it
method. If the method exists, call it:
This is a better design anyway: relying on type information is much more brittle.
这篇关于isinstance 和 Mocking的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!