【发布时间】:2020-07-19 18:00:59
【问题描述】:
我正在使用 Cypress 编写集成测试。 Cypress 使用 Sinon 作为存根和间谍,所以 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;
});
});
我错过了什么吗?
【问题讨论】:
-
console.log(internationals)打印什么? -
我添加了截图@glebbahmutov
-
奇怪的是同时存在
getInternationals和getter 属性get getInternationals -
是的,我不知道为什么会这样
-
getInternationals在控制台中显示工具提示“调用属性获取器”。get getInternationals引用似乎是 getter 函数本身,您可以通过const getterFunction = Object.getOwnPropertyDescriptor(internationals, "getInternationals").get;访问它。
标签: javascript testing integration-testing sinon cypress