首页 > 解决方案 > 如何使用 toHaveBeenCalledWith 仅检查 URL?

问题描述

我正在测试一个 API,并想检查是否使用了正确的 api URL。现在看来我可以使用toHaveBeenCalledWith了。但是,它会返回该请求选项中的所有内容:

Expected: "http://localhost:5000/myUrls"
Received: "http://localhost:5000/myUrl", {"headers": {"Authorization": "Bearer foobar", "Content-Type": "application/json", "X-Request-ID": "15863752509339714997334447706"}}

仅检查 url 的常见做法是什么?

标签: reactjsjestjs

解决方案


您可以将第二个参数指定为任何值:

expect(fn).toHaveBeenCalledWith(
    "http://localhost:5000/myUrls",
    expect.anything()
);

见这里:https ://jestjs.io/docs/en/expect#expectanything


推荐阅读