[英]Unit test Angular 4 getting error `TypeError: Cannot read property 'subscribe' of undefined`
[英]Karma unit test EventEmitter emit and subscribe failed, Cannot read property 'subscribe' of undefined
我是 Angular/Node UT 的新手,目前,我添加了 karma 和 jasmine 来运行单元测试,我的应用程序基于 Angular7 + Node 8,遵循Angular 测试文档,大多数测试用例运行成功,但有些情况像@Input
@Output
( EventEmitter
) 数据测试失败。 请查看我的代码并配置如下:
package.json
:
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.0.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2"
a.component.ts
:
userDetailInit(){
this.lookupService.genericServiceCall1(this.componentIds.componentUserDetail);
this.lookupService.compomentJsonEmit.subscribe(async o => {
// logic
}, error => {
this.error("System Error");
});
}
lookupService
:
genericServiceCall1(iD) {
let param = {
'id': iD
};
this.compomentJsonEmit = new EventEmitter<Object>();
// getData() is a http request and will return data
this.commonService.getData('Get', param).subscribe(o =>{
this.compomentJsonEmit.emit(o);
});
}
在单元测试文件agent-details.component.spec.ts
中:
describe('UserDetailsComponent', () => {
let spy = {
compJSONSpy: jasmine.createSpyObj('lookupService', ['genericServiceCall1']),
}
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ UserDetailsComponent ],
imports: [FormlyModule.forRoot(), HttpClientTestingModule],
providers: [
{provide: CommonService, useValue: spy.comSpy },
{provide: LookupService, useValue: spy.compJSONSpy}
],
schemas: [NO_ERRORS_SCHEMA ]
}).compileComponents();
fixture = TestBed.createComponent(UserDetailsComponent);
component = fixture.componentInstance;
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
}));
it('#timeChange(val) should set selectTimeZone to val', () => {
compJSONSpyReturn = spy.compJSONSpy.genericServiceCall1.and.returnValue(of(spyReturnJson.UserDetailCompJson));
component.componentIds = {
componentUserDetail: 1007
};
// error occurred when run component.timeChange(12);
component.timeChange(12);
expect(compJSONSpyReturn.calls.any()).toBeDefined();
expect(component.selectTimeZone).toEqual(12);
});
});
因果报应报告:
TypeError: Cannot read property 'subscribe' of undefined
at userDetailsComponent.subscribe [as userDetailInit] (http://localhost:9876/_karma_webpack_/webpack:/src/app/admin/user-details/user-details.component.ts:77:46)
at userDetailsComponent.userDetailInit [as timeChange] (http://localhost:9876/_karma_webpack_/webpack:/src/app/admin/user-details/user-details.component.ts:109:14)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/admin/user-details/user-details.component.spec.ts:66:15)
userListComponent #ngOninit() should be run
this.lookupService.compomentJsonEmit
undefined
。
尝试:
const compomentJsonEmitSubject = new BehaviorSubject('hello'); // you can mock it how you like instead of 'hello'
describe('UserDetailsComponent', () => {
let spy = {
compJSONSpy: jasmine.createSpyObj('lookupService', ['genericServiceCall1']),
}
spy.compJSONspy.compomentJsonEmit = componentJsonEmitSubject;
beforeEach(async(() => {
....
然后稍后在您的测试中,如果您想要一个新的componentJsonEmit
值,您可以执行componentJsonEmitSubject.next('goodbye');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.