简体   繁体   English

什么是spec和spec_set

[英]What is spec and spec_set

I am using Mock 1.0.1 python.我正在使用 Mock 1.0.1 python。 In the path function definition there are two optional arguments names spec and spec_set (also auto_spec)在路径函数定义中有两个可选参数名称spec和spec_set(也是auto_spec)

patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)

I have read through the documentation, but find no explanation of them.我已经通读了文档,但没有找到对它们的解释。 Maybe they are terms of testing?也许它们是测试条款? It will be nice if someone can give information, thank you.如果有人能提供信息就太好了,谢谢。

unittest.mock in Python 3.x is basically same with mock . Python 3.x 中的unittest.mockmock基本相同。

According to the unittest.mock documentation:根据unittest.mock文档:

spec : This can be either a list of strings or an existing object (a class or instance) that acts as the specification for the mock object. spec :这可以是字符串列表,也可以是充当模拟对象规范的现有对象(类或实例)。 If you pass in an object then a list of strings is formed by calling dir on the object (excluding unsupported magic attributes and methods).如果您传入一个对象,则通过在该对象上调用 dir 来形成一个字符串列表(不包括不受支持的魔法属性和方法)。 Accessing any attribute not in this list will raise an AttributeError.访问不在此列表中的任何属性将引发 AttributeError。

If spec is an object (rather than a list of strings) then _ class _ returns the class of the spec object.如果 spec 是一个对象(而不是字符串列表),则 _ class _ 返回 spec 对象的类。 This allows mocks to pass isinstance tests.这允许模拟通过 isinstance 测试。

spec_set : A stricter variant of spec. spec_set :规范的更严格变体。 If used, attempting to set or get an attribute on the mock that isn't on the object passed as spec_set will raise an AttributeError.如果使用,尝试在模拟上设置或获取不在作为 spec_set 传递的对象上的属性将引发 AttributeError。


Update Difference between spec and spec_set .更新specspec_set之间的差异。

With spec , you can set attribute that is not specified, while with spec_set , it is not allowed to set unspecified attribute.使用spec可以设置未指定的属性,而使用spec_set则不允许设置未指定的属性。

Example:例子:

>>> from unittest.mock import Mock
>>> class A:
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b
...
>>> aobj = A(1, 2)



>>> m = Mock(spec=aobj)   # spec
>>> m.c   # get -> fail
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python3/3.6.0b4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 582, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'c'
>>> m.c = 9  # set -> success
>>> m.c      # get -> success (although c is not in the spec)
9



>>> m = Mock(spec_set=aobj)   # spec_set
>>> m.a
<Mock name='mock.a' id='4544967400'>
>>> m.b
<Mock name='mock.b' id='4545493928'>
>>> m.c   # get -> fail
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python3/3.6.0b4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 582, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'c'
>>> m.c = 9  # set -> fail
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python3/3.6.0b4_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/mock.py", line 688, in __setattr__
    raise AttributeError("Mock object has no attribute '%s'" % name)
AttributeError: Mock object has no attribute 'c'

You can find more info here: http://www.voidspace.org.uk/downloads/mock-1.0.1.pdf您可以在此处找到更多信息: http : //www.voidspace.org.uk/downloads/mock-1.0.1.pdf

• spec: This can be either a list of strings or an existing object (a class or instance) that acts as the specification for the mock object. • 规范:这可以是字符串列表或充当模拟对象规范的现有对象(类或实例)。 If you pass in an object then a list of strings is formed by calling dir on the object (excluding unsupported magic attributes and methods).如果您传入一个对象,则通过在该对象上调用 dir 来形成一个字符串列表(不包括不受支持的魔法属性和方法)。 Accessing any attribute not in this list will raise an AttributeError.访问不在此列表中的任何属性将引发 AttributeError。 If spec is an object (rather than a list of strings) then class returns the class of the spec object.如果 spec 是一个对象(而不是字符串列表),则class返回 spec 对象的类。 This allows mocks to pass isinstance tests.这允许模拟通过 isinstance 测试。

• spec_set: A stricter variant of spec. • spec_set:规范的更严格变体。 If used, attempting to set or get an attribute on the mock that isn't on the object passed as spec_set will raise an AttributeError.如果使用,尝试在模拟上设置或获取不在作为 spec_set 传递的对象上的属性将引发 AttributeError。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM