在angular4中对@viewChild进行单元测试

时间:2017-11-22 05:00:09

标签: angular unit-testing karma-jasmine

我刚接触单元测试。我试着编写@viewChild的测试来定义与否。

describe('App Component', () => {
  let component: AppComponent;
  let fixture: ComponentFixture<AppComponent>;
  let debugElement: DebugElement;
  let htmlElement: HTMLElement;
  let testFixture: ComponentFixture<AppTestComponent>;
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ],
      imports: [
        HttpClientModule
      ],
      providers: [FileUploadService, HttpClientModule]
    }).compileComponents();
  }));

  beforeEach(async(() => {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    debugElement = fixture.debugElement;
    htmlElement = debugElement.nativeElement;
  }));

  beforeEach(() => {
    testFixture = TestBed.createComponent(AppTestComponent);
  });

  it('will have the both `ViewChild`s defined', () => {
    expect(testFixture.componentInstance.fileInputOne).toBeDefined();
    expect(testFixture.componentInstance.fileInputTwo).toBeDefined();
  });


});


class AppTestComponent {
  @ViewChild('fileInputOne') fileInputOne;
  @ViewChild('fileInputTwo') fileInputTwo;

}

但这不起作用。这是为@viewChild编写单元测试的正确方法吗? 有人可以参考有趣的网站来学习angular4单元测试吗? 什么

0 个答案:

没有答案