Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular expect(spy).TohaveBeenCalledTime(expected)预期呼叫数:1收到呼叫数:0_Angular_Unit Testing_Jestjs - Fatal编程技术网

Angular expect(spy).TohaveBeenCalledTime(expected)预期呼叫数:1收到呼叫数:0

Angular expect(spy).TohaveBeenCalledTime(expected)预期呼叫数:1收到呼叫数:0,angular,unit-testing,jestjs,Angular,Unit Testing,Jestjs,首先,如果这很简单,我很抱歉这是我第一次编写单元测试。我编写代码来测试在这种情况下fn是否被调用过一次 it("fill shownCategories when state not compare", () => { component.state != 'compare' if (component.state != 'compare') { const filterShownCategoriesSpy = spyOn(component, 'F

首先,如果这很简单,我很抱歉这是我第一次编写单元测试。我编写代码来测试在这种情况下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);
  }
}
输出 期望(间谍)。期望已被催缴的时间(期望)


在这里,首先,你们是以错误的方式给状态赋值

改变这个

 component.state != 'compare';

并在第行下方添加

component.addFirstTable();
以前

expect(filterCategoriesSpy).toHaveBeenCalledTimes(1);

请提供可以重现问题的。不清楚您发布的代码片段与测试有何关联,也不清楚
component.state!='在测试中比较'if(component.state!='compare'){
。测试失败,因为在
spyOn
expect(filtershowncategoriespy)之间
为了通过测试,应该有调用
FilterhownCategories
的代码,目前没有,当然没有调用。如果这样做有效,请也接受这个答案。谢谢。
component.addFirstTable();
expect(filterCategoriesSpy).toHaveBeenCalledTimes(1);