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 8_Angular_Unit Testing_Mocking_Karma Jasmine_Angular Directive - Fatal编程技术网

模拟测试组件的指令-带茉莉花和卡玛的Angular 8

模拟测试组件的指令-带茉莉花和卡玛的Angular 8,angular,unit-testing,mocking,karma-jasmine,angular-directive,Angular,Unit Testing,Mocking,Karma Jasmine,Angular Directive,我试图为具有一些服务和指令的组件编写一个单元测试。该指令用于呈现按登录用户和来宾用户区分的内容 对于“appShowAuthed”,mock指令的默认值为“false”,测试用例通过。但当我尝试更改该值时,它不会重新渲染视图 HTML: 错误:“TypeError:无法读取未定义的属性“startsWith” 我已经在项目中添加了最少的代码,以便在gitHub中重现这个问题 请给我一个更好的方法来解决这个问题 提前感谢尝试以下操作: it('should show Login button

我试图为具有一些服务和指令的组件编写一个单元测试。该指令用于呈现按登录用户和来宾用户区分的内容

对于“appShowAuthed”,mock指令的默认值为“false”,测试用例通过。但当我尝试更改该值时,它不会重新渲染视图

HTML:

错误:“TypeError:无法读取未定义的属性“startsWith”

我已经在项目中添加了最少的代码,以便在gitHub中重现这个问题

请给我一个更好的方法来解决这个问题

提前感谢

尝试以下操作:

 it('should show Login button and hide My Orders Button for not logged in user', inject(
    [DataStoreService],
    async (injectService: DataStoreService) => {
      fixture.autoDetectChanges(true);
      injectService.isAuthenticated.next(true);
      await fixture.whenStable();
      await fixture.whenRenderingDone();
      expect(
        fixture.debugElement.query(By.css('#logout')),
      ).not.toBeNull();
      expect(fixture.debugElement.query(By.css('#login'))).toBeNull();
    },
  ));
//Passing Case

  it('should show Login button and hide My Orders Button for not logged in user', async () => 
  {
    await fixture.detectChanges();
    expect(fixture.debugElement.query(By.css('#login'))).not.toBeNull();
    expect(fixture.debugElement.query(By.css('#logout'))).toBeNull();
  });



//Failing Case

 it('should show Login button and hide My Orders Button for not logged in user', async () => 
    {
       const datastore = TestBed.get(DataStoreService);
       datastore.isAuthenticated = of(true);

       fixture.detectChanges();
       await fixture.whenStable();
       await fixture.whenRenderingDone();
       expect(fixture.debugElement.query(By.css('#logout'))).not.toBeNull();
       expect(fixture.debugElement.query(By.css('#login'))).toBeNull();
    });
 it('should show Login button and hide My Orders Button for not logged in user', inject(
    [DataStoreService],
    async (injectService: DataStoreService) => {
      fixture.autoDetectChanges(true);
      injectService.isAuthenticated.next(true);
      await fixture.whenStable();
      await fixture.whenRenderingDone();
      expect(
        fixture.debugElement.query(By.css('#logout')),
      ).not.toBeNull();
      expect(fixture.debugElement.query(By.css('#login'))).toBeNull();
    },
  ));