测试使用继承的参数化构造函数

发布于 2025-01-10 15:22:57 字数 1584 浏览 0 评论 0原文

我想测试如下所示的函数

async def post_lookup(self, request: web.Request):
    response = await ZipcodeLookupModel(
        request.app[constants.CONNECTOR], session_store=request._state[constants.SESSION_STORAGE]
    ).lookup(request=request)
    return web.json_response(response)

ZipcodeLookupModel 及其层次结构如下所示

class ZipcodeLookupModel(BaseModel):
    def __init__(self, *args, **kwargs):
        super(ZipcodeLookupModel, self).__init__(*args, **kwargs)
        
class BaseModel:
    def __init__(self, connector=None, session_store=None):
        self.session_store = session_store
        self.connector = connector
        self.loop = get_running_loop()
        self.service_args = dict(connector=self.connector, loop=self.loop)

我想修补该对象创建

    with patch(
        "src.api.v1.zipcode.lookup.model.ZipcodeLookupModel",
        new=ZipcodeLookupModelStub()
    ):
        response = await client.session.post(url=f"http://{client.host}:{client.port}{path}", json=self.message)
        assert response.status == 200
        data = await response.json()
        assert data

我已经为测试创建了一个存根对象

class ZipcodeLookupModelStub(BaseModel):
    def __init__(self, *args, **kwargs):
        super(ZipcodeLookupModelStub, self).__init__(*args, **kwargs)

    async def lookup(self, request):

但是当我运行测试时,出现此错误(在 post_lookup 中创建对象的行上),

super() argument 1 must be type, not ZipcodeLookupModelStub

问题是什么?

I want to test the function that looks like this

async def post_lookup(self, request: web.Request):
    response = await ZipcodeLookupModel(
        request.app[constants.CONNECTOR], session_store=request._state[constants.SESSION_STORAGE]
    ).lookup(request=request)
    return web.json_response(response)

The class ZipcodeLookupModel and its hierarchy looks like this

class ZipcodeLookupModel(BaseModel):
    def __init__(self, *args, **kwargs):
        super(ZipcodeLookupModel, self).__init__(*args, **kwargs)
        
class BaseModel:
    def __init__(self, connector=None, session_store=None):
        self.session_store = session_store
        self.connector = connector
        self.loop = get_running_loop()
        self.service_args = dict(connector=self.connector, loop=self.loop)

I want to patch that object creation

    with patch(
        "src.api.v1.zipcode.lookup.model.ZipcodeLookupModel",
        new=ZipcodeLookupModelStub()
    ):
        response = await client.session.post(url=f"http://{client.host}:{client.port}{path}", json=self.message)
        assert response.status == 200
        data = await response.json()
        assert data

I have created a stub object for the test

class ZipcodeLookupModelStub(BaseModel):
    def __init__(self, *args, **kwargs):
        super(ZipcodeLookupModelStub, self).__init__(*args, **kwargs)

    async def lookup(self, request):

But when I run the test I get this error (on the line where the object is created in post_lookup)

super() argument 1 must be type, not ZipcodeLookupModelStub

What is the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

池予 2025-01-17 15:22:57

您要将名称 ZipCodeLookupModel 替换为您要使用的类的实例,而不是类本身。

with patch(
    "src.api.v1.zipcode.lookup.model.ZipcodeLookupModel",
    new=ZipcodeLookupModelStub  # No parentheses
):
    response = await client.session.post(url=f"http://{client.host}:{client.port}{path}", json=self.message)
    assert response.status == 200
    data = await response.json()
    assert data

You are replacing the name ZipCodeLookupModel with an instance of the class you want to use, not the class itself.

with patch(
    "src.api.v1.zipcode.lookup.model.ZipcodeLookupModel",
    new=ZipcodeLookupModelStub  # No parentheses
):
    response = await client.session.post(url=f"http://{client.host}:{client.port}{path}", json=self.message)
    assert response.status == 200
    data = await response.json()
    assert data
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文