我正在使用 Cypress 编写集成测试。 Cypress 使用 Sinon 作为 stub 和 spy ,所以 cy.spy = sinon.spy。
我正在尝试从导入的模块中监视方法。
我正在像这样导入模块:import * as internationals from "../../../src/api/Internationals";
然后我尝试窥探api/Internationals
中的一个方法。像这样:cy.spy(internationals, "getInternationals");
导致此错误:
TypeError: Attempted to wrap undefined property getInternationals as function
api/Internationals.js
的内容:import ApiClient from "./ApiClient";
export const getInternationals = ({ search, team, from, till, page = 1, per_page = 10, sort_column, sort_direction }) =>
ApiClient.get(`/players`, {
params: {
search: search ? (search === "#" ? "hek" : search) : null,
team: team ? team : null,
from: from ? from : null,
till: till ? till : null,
page: page,
per_page: per_page,
sort_column: sort_column ? sort_column : null,
sort_direction: sort_direction ? sort_direction : null
}
});
测试内容:
/// <reference types="Cypress" />
import * as internationals from "../../../src/api/Internationals";
describe("Can search for internationals", () => {
it("Remembers filters after refreshing", () => {
console.log(internationals);
cy.spy(internationals, "getInternationals");
expect(internationals.getInternationals).to.be.called;
});
});
控制台日志
internationals
显示:我错过了什么吗?
最佳答案
跟踪控制台中的错误,很明显 Cypress 没有处理 export const getInternationals
构造正确,但不能一目了然。
注意:重新定位导入不起作用,留待讨论
您可以通过在测试中像这样进行 spy 事件来躲避问题
const spyBase = {
getInternationals: internationals.getInternationals
}
cy.spy(spyBase, "getInternationals");
expect(spyBase.getInternationals).to.be.called;
如果您可以更改源代码,请将默认导出添加到模块
export const getInternationals = ...
export default {
getInternationals
}
并像这样导入它
import internationals from "../../../src/api/Internationals";
关于javascript - Sinon TypeError : Attempted to wrap undefined property getInternationals as function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61081814/