1

我有一个正在测试的系统 B,它使用一些 A 和一些我想模拟的昂贵功能,但 B 依赖于获取 A 的多个实例。

http://docs.python.org/3/library/unittest.mock.html#unittest.mock.patch说:“如果类被实例化多次,你可以使用 side_effect 每次返回一个新的模拟。” 但我不明白,如何:(

我将如何更改此示例:

import time

class A:
    def say_hi(self):
        print("lets do something horribly expensive")
        time.sleep(2)

class B:
    def __init__(self):
        self.a_map = {}
    def get_a(self):
        a = A()
        a.say_hi()
        self.a_map[a] = True

with patch('__main__.A') as mockA:
    b = B()
    def side_effect():
        # return DEFAULT  # no success
        return mockA  # no success neither
        # return mockA.clone()  # :(
    mockA.side_effect = side_effect  # no help
    # mockA.side_effect = [DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT]  # nope
    # mockA.side_effect = [mockA, mockA, mockA, mockA, mockA]  # nope
    # mockA.side_effect = [DEFAULT, mockA, mockA, mockA, mockA]  # nope, but hey! I get 2 instances in my key set now!
    # mockA.side_effect = [DEFAULT, mockA, mockA.clone(), mockA.clone().clone(), mockA.clone().clone().clone()]  # would work, but hey, seriously?

    for _ in range(5):
        b.get_a()
    print(b.a_map)
    assert len(b.a_map) == 5

产生这个输出:

{<MagicMock name='A' id='139998588645520'>: True}
Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
AssertionError

(不模拟断言成功,但需要 10 秒。)

4

1 回答 1

3

那你在嘲笑错误的事情。如果 say_hi 是你想模拟掉的昂贵部分,那么模拟它。然后你就不必弄乱副作用了。

with patch('__main__.A.say_hi'):
    b = B()
    for _ in range(5):
        b.get_a()
    print(b.a_map)
    assert len(b.a_map) == 5

要明确回答“使用 side_effects 创建单独的模拟实例”的问题,您可以将 Mock 指定为要返回的对象。

from mock import Mock
with patch('__main__.A') as mockA:
    b = B()
    mockA.side_effect = Mock
    for _ in range(5):
        b.get_a()
    print(b.a_map)
    assert len(b.a_map) == 5
于 2013-08-23T01:22:31.160 回答