如何在Python / Flask中模拟init函数?

时间:2019-01-10 02:58:32

标签: python pytest python-mock

说我有一个看起来像这样的课:

class ThingToMock(Command):
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

它基本上是数据的读取器/容器。我该如何在pytest中模拟呢?说它所在的路径是文件夹d/c/e

我有这个:

@pytest.fixture(autouse=True)
def mock_thing(mocker):
    return mocker.patch(
        'd.c.e',
        return_value={}
    )

@pytest.fixture(autouse=True)
def mock_service_thing(mocker):
    return mocker.patch(
        'd.c.e.f.RealService',
    )

我后来想在View中写一个测试,内容为:

client.get(f'some/path')

mock_service_thing.assert_called_once_with(mock_thing.a, mock_thing.b, mock_thing.c)

我该怎么做?

0 个答案:

没有答案