Python实际类在单元测试用例中使用myfakeclass进行了模拟,但找不到assert_Called_with

问题描述

在下面的测试案例中,我使用 FakeClient类模拟了实际的 Client类

self.mock_client = mock.patch('operation.Client',new=FakeClient)

此self.mock_client没有与之关联的 assert_call_with 函数。我尝试过的代码:

operation.py:

import requests

class Operation:
    def __init__(self,ip,user,password):
        self.ip = ip
        self.cl = Client(user,password)

    def _prepare_payload_for_create(self):
        return {'name': 'myentity'}

    def create_op(self):
        data = _prepare_payload_for_create()
        self.cl.request("POST","http://localhost/create_entity",data=data)

class Client:
    def __init__(self,password):
        self.user = user
        self.password = password
        self.headers = {}

    def request(self,method,url,data=None,params=None):
        if data:
            resp = requests.request(method,data=data,headers=self.headers)
        else:
            resp = requests.request(method,headers=self.headers)
        return resp

my_test.py:

import mock
from testtools import TestCase

class TestOperation(TestCase):
    def setUp(self):
        self.mock_client = mock.patch('operation.Client',new=FakeClient)
        self.mock_client.start()
        self.myop = Operation("TestUser","TestPassword")

    def test_create_op(self):
        resp = self.myop.create_op
        # self.mock_client do not have assert_called_with

请提出如何获得与self.mock_client关联的assert_call_with

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...