Karma 单元测试 EventEmitter 发射和订阅失败,无法读取未定义的 属性 'subscribe'
Karma unit test EventEmitter emit and subscribe failed, Cannot read property 'subscribe' of undefined
我是 Angular/Node UT 的新手,目前,我将 karma 和 jasmine 添加到 运行 单元测试中,我的应用程序基于 Angular7 + Node 8,遵循 Angular test docs,大多数测试用例 运行 成功,但某些情况如 @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');
我是 Angular/Node UT 的新手,目前,我将 karma 和 jasmine 添加到 运行 单元测试中,我的应用程序基于 Angular7 + Node 8,遵循 Angular test docs,大多数测试用例 运行 成功,但某些情况如 @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');