Expected =1, received=0 - 为什么我的函数没有在 Angular 测试中调用?
Expected =1, received=0 - why is my function not called in Angular test?
我检查控制台 .. shownCategories 不为空,但在测试中为空我正在尝试检查 FilterShownCategories() 是否被调用但其输出 received=0, expected=1
it("in case edit should call filterCategories()", () => {
component.state == 'edit';
if (component.state == 'edit') {
const filterCategoriesSpy = spyOn(component, 'filterCategories').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(filterCategoriesSpy).toHaveBeenCalledTimes(1);
}
});
}}
这是函数 :
public addFirstTable() {
console.log("addFirstTable");
//awel mara showncategories gaya fadya a lazm amlaha lw state msh edit
if (this.state != "compare") {
// this.shownCategories = this.allCategories.map(a => ({ ...a }));
this.shownCatgeories = this.FilterShownCategories();
console.log("shownCategories if state is not compare", this.shownCategories);
}
}
输出:
expected=1, received=0
我想你忘了执行测试函数。
添加下行
component.addFirstTable();
在此之前
expect(filterCategoriesSpy).toHaveBeenCalledTimes(1);
我检查控制台 .. shownCategories 不为空,但在测试中为空我正在尝试检查 FilterShownCategories() 是否被调用但其输出 received=0, expected=1
it("in case edit should call filterCategories()", () => {
component.state == 'edit';
if (component.state == 'edit') {
const filterCategoriesSpy = spyOn(component, 'filterCategories').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(filterCategoriesSpy).toHaveBeenCalledTimes(1);
}
});
}}
这是函数 :
public addFirstTable() {
console.log("addFirstTable");
//awel mara showncategories gaya fadya a lazm amlaha lw state msh edit
if (this.state != "compare") {
// this.shownCategories = this.allCategories.map(a => ({ ...a }));
this.shownCatgeories = this.FilterShownCategories();
console.log("shownCategories if state is not compare", this.shownCategories);
}
}
输出:
expected=1, received=0
我想你忘了执行测试函数。
添加下行
component.addFirstTable();
在此之前
expect(filterCategoriesSpy).toHaveBeenCalledTimes(1);