角度单位测试 - 找不到姓名' xyzDirective'

时间:2018-05-30 13:56:40

标签: angular unit-testing angular5 karma-jasmine

我有一个使用模块加载器的组件,当我将其导入我的spec文件时,我似乎无法测试特定的指令,因为它无法找到。我得到了

Cannot find name 'AwesomeHelperDirective'

并且Jasmine输出说:

ReferenceError: AwesomeHelperDirective is not defined

我的模块看起来像这样......

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import {
  AwesomeComponent,
} from './awesome.component';

@NgModule({
  declarations: [
      AwesomeComponent,
      AwesomeHelperDirective,
  ],
  imports: [CommonModule],
  exports: [
      AwesomeComponent,
      AwesomeHelperDirective,
  ]
})
export class AwesomeModule { }

在我的spec文件中,我有这个:

import { AwesomeModule } from './awesome.module';

describe('AwesomeComponent', () => {

   const html =  `<app-template></app-template>`;

   beforeEach(() => {
      TestBed.configureTestingModule({
        imports: [AwesomeModule],
        declarations: [TestComponent],
      }).overrideComponent(TestComponent, {
          set: {template: html},
        }).compileComponents();
    });

    it('should ...', () => {
      const fixture = TestBed.createComponent(TestComponent);
      fixture.detectChanges();

      const clickMock = spyOn(AwesomeHelperDirective, 'onClick');
      fixture.debugElement.query(By.css('awesome-helper')).triggerEventHandler('click', null);
      expect(clickMock).toHaveBeenCalled();
    });

});

0 个答案:

没有答案