业力测试EventEmitter发出和订阅失败,无法读取未定义的属性'subscribe'

问题描述 投票:0回答:1

[我不是Angular / Node UT的新手,目前,我添加了业力和茉莉花来运行单元测试,我的应用程序基于Angular7 + Node 8,在Angular test docs之后,大多数测试用例都成功运行,但是某些情况下[ C0] @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

Karma ussye报告:

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);
  });
});
javascript node.js angular karma-jasmine karma-coverage
1个回答
0
投票

[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

© www.soinside.com 2019 - 2024. All rights reserved.