【发布时间】:2021-05-05 02:06:17
【问题描述】:
我正在编写单元测试的 Angular 应用程序。我的组件中有一种方法。
我的组件:
route(element) {
this.myService.Name(element.Name);
this.myService.Code(element.Code);
this.router.navigate(["myPage"], { relativeTo: this.routes});
}
我的服务:
Name(data) {
this.name.next(data);
}
Code(data) {
this.code.next(data);
}
private code = new BehaviorSubject("");
getCode = this.code.asObservable();
private name= new BehaviorSubject("");
getName = this.name.asObservable();
我的单元测试如下:
it('route() function should be called', () => {
component.route(mockData[0]);
var spy = spyOn(component, "route").and.callThrough();
expect(spy).toBeDefined();
})
我在运行测试用例时遇到以下错误:
TypeError: this.myService.Name 不是函数
我该如何解决这个问题?
【问题讨论】:
-
你必须模拟
myService。你有TestBed.configureTestingModule吗?
标签: angular jasmine karma-jasmine angular8