Python 无法模拟restplus api合作者

Python 无法模拟restplus api合作者,python,flask,flask-restplus,Python,Flask,Flask Restplus,我试图模拟在flask restplus API定义中调用的函数。下面是包含API定义的文件 from flask_restplus import Namespace, Resource def some_func() -> str: return "abcd" ns = Namespace("MyApi") @ns.route("get_value") class TestApi(Resource):

我试图模拟在flask restplus API定义中调用的函数。下面是包含API定义的文件

from flask_restplus import Namespace, Resource


def some_func() -> str:
    return "abcd"


ns = Namespace("MyApi")


@ns.route("get_value")
class TestApi(Resource):
    def get(self):
        return some_func()
我可以测试完整的函数,但似乎无法正确地模拟
某些函数

from unittest import mock
from unittest.mock import patch

from flask import url_for


def test_my_api_static(client):
    """
    Test real return value: this passes

    Args:
        client (): mocked flask runtime provided by pytest-flask
    """
    result = client.get(url_for(("MyApi_test_api")))
    assert result.status_code == 200
    assert result.data == b'"abcd"\n'


@patch("apis.my_api.my_api.some_func")
def test_my_api_mockedclient1(mock_fn, client):
    """
    This fails:
    thing = <flask_restplus.namespace.Namespace object at 0x114ac4bd0>
    comp = 'my_api', import_path = 'apis.my_api.my_api'

        def _dot_lookup(thing, comp, import_path):
            try:
                return getattr(thing, comp)
            except AttributeError:
                __import__(import_path)
    >           return getattr(thing, comp)
    E           AttributeError: 'Namespace' object has no attribute 'my_api'

    /usr/local/opt/python@3.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py:1155: AttributeError
    """
    mock_fn.return_value = "xyz"
    result = client.get(url_for(("MyApi_test_api")))
    assert result.status_code == 200
    assert result.data == b'"xyz"\n'


def test_my_api_mockedclient2(client):
    """
    This fails:
    >       assert result.data == b'"xyz"\n'
    E       assert b'"abcd"\n' == b'"xyz"\n'
    E         At index 1 diff: b'a' != b'x'
    E         Use -v to get the full diff
    """
    mock_fn = patch("apis.my_api.my_api.some_func")
    mock_fn.return_value = "xyz"
    result = client.get(url_for(("MyApi_test_api")))
    assert result.status_code == 200
    assert result.data == b'"xyz"\n'
from apis.my_api import my_api


@patch.object(my_api, "some_func")
def test_m(mock_fn, client):
    mock_fn.return_value = "xyz"
    result = client.get(url_for(("MyApi_test_api")))
    assert result.status_code == 200
    assert result.data == b'"xyz"\n'