【发布时间】:2020-11-19 05:10:24
【问题描述】:
第一次如果很简单,对不起这是我第一次写单元测试。我写我的代码来测试fn是否在条件下被调用一次
it("fill shownCategories when state not compare", () => {
component.state != 'compare'
if (component.state != 'compare') {
const filterShownCategoriesSpy = spyOn(component, 'FilterShownCategories').and.returnValue(
[{
hiddenVariants: 0,
id: 187910,
lang: "de",
name: "AMG/Exterieur",
plId: 5389,
refId: "1015001000",
type: "AMG",
visibleVariants: 5
}, {
hiddenVariants: 0,
id: 187911,
lang: "de",
name: "AMG/Interieur",
plId: 5389,
refId: "1015002000",
type: "AMG",
visibleVariants: 7,
}]
)
expect(filterShownCategoriesSpy).toHaveBeenCalledTimes(1);
expect(component.shownCatgeories).toEqual([]);
}
});
public addFirstTable() {
console.log("addFirstTable");
//awel mara showncategories gaya fadya a lazm amlaha lw state msh edit
if (this.state != "compare") {
// this.shownCatgeories = this.allCatigories.map(a => ({ ...a }));
this.shownCatgeories = this.FilterShownCategories();
console.log("shownCatgeories if state is not compare", this.shownCatgeories);
}
}
输出 期望(间谍).toHaveBeenCalledTimes(期望)
Expected number of calls: 1
Received number of calls: 0
【问题讨论】:
-
请提供可以重现问题的stackoverflow.com/help/mcve。不清楚你发的sn-p和测试有什么关系,也不清楚
component.state != 'compare' if (component.state != 'compare') {在测试中的目的是什么。测试失败是因为在spyOn和expect(filterShownCategoriesSpy)之间应该有调用filterShownCategories的代码才能通过测试,而目前没有,肯定没有调用。
标签: angular unit-testing jestjs