如何断言在pytest中使用模拟对象调用方法?

时间:2019-01-10 17:56:22

标签: pytest

我有一个看起来像这样的真实方法:

   def patch(self, policy_id, transaction_id):
        command = SomeCommand(id, submission_data=request.json)
        print("==========should be mock command========", command)
        try:
            return self._service.call(command), 200
        except ResponseError as error:
            abort(500, message=str(error))

我有一个看起来像这样的测试,但是没有用,我也不知道为什么。因为命令不是模拟的,所以它不起作用:

@pytest.fixture(autouse=True)
def mock_command(mocker):
    return mocker.patch.object(
        SomeCommand,
        "__init__",
        return_value=None
    )


@pytest.fixture(autouse=True)
def mock_service(mocker):
    return mocker.patch(
        'app.RealService.some_real_method',
        return_value=True

  def test_calls_ervice_with_command(
            self, client, id, mock_service):
        response = client.patch(       f'some_route/is/this')

        mock_service.assert_called_once_with(mock_command)

我在做什么错?看到什么了吗?

0 个答案:

没有答案