Angular 4测试从@viewChild调用另一个方法的方法

时间:2018-03-30 13:32:38

标签: angular

我有一个getFirstLocation()方法正在调用另一个组件@ViewChild

组件声明如下:

export class CreateGroupComponent implements OnInit {
  @ViewChild(MapComponent) mapcomponent: MapComponent;
  getFirstLocation(location){
    this.mapcomponent.getLocation();
  }
}

测试用例(.spc):

beforeEach(async(() => {
TestBed.configureTestingModule({
  imports: [
    ReactiveFormsModule,
    FormsModule,
    BrowserAnimationsModule
  ],
  declarations: [CreateGroupComponent, MapComponent],
  schemas: [NO_ERRORS_SCHEMA],
  providers: [
    GroupService,
  ]
})
  .compileComponents();
}));
beforeEach(() => {
  fixture = TestBed.createComponent(CreateGroupComponent);
  component = fixture.componentInstance;
});

it('should create getFirstLocation successfully', () => {
  spyOn(timepickerChild, 'getLocation').and.returnValue(true);
  spyOn(component.mapcomponent, 'getLocation').and.returnValue(true);
  component.getFirstLocation('TestData');
  expect(component.getFirstLocation).toBeTruthy();
  expect(timepickerChild.getLocation).toHaveBeenCalled();
});

错误::找不到要监视getLocation()的对象 谢谢你的帮助。

0 个答案:

没有答案