[英]Angular Unit Testing- Mocking of Service in component
I am new to angular testing, and trying to implement a basic testing of mock service with Angular.我是角度测试的新手,并尝试使用 Angular 实现模拟服务的基本测试。 I get the 'Cannot read subscribe of undefined' when calling personalsettingmock.getPersonalSettings app.component.spec.ts
调用personalsettingmock.getPersonalSettings app.component.spec.ts 时出现“无法读取未定义订阅”
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { PersonalSettingsComponent } from './personal-settings.component';
import { PersonalSettingsService } from './personal-settings.service';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { NotifyService } from 'src/app/_services/notify.service';
import { LoaderEmitterService } from 'src/app/_services/loader-emitter.service';
import { of } from 'rxjs/internal/observable/of';
fdescribe('PersonalSettingsComponent', () => {
let component: PersonalSettingsComponent;
let fixture: ComponentFixture<PersonalSettingsComponent>;
let personalSettingsServiceMock;
let notifyServiceMock;
let loaderEmitterServiceMock;
let data = {
email: 'asd', firstName: 'sff', lastName: 'sdf', phoneMobile: '+12355'
};
beforeEach(async(() => {
personalSettingsServiceMock = jasmine.createSpyObj(['getPersonalSettings']);
notifyServiceMock = jasmine.createSpyObj(['notifyUser']);
loaderEmitterServiceMock = jasmine.createSpyObj('LoaderEmitterSerivce', ['emitChange']);
TestBed.configureTestingModule({
declarations: [PersonalSettingsComponent],
imports: [
],
providers: [
{
provide: NotifyService,
useValue: notifyServiceMock
},
{
provide: LoaderEmitterService,
useValue: loaderEmitterServiceMock
}
],
schemas: [
NO_ERRORS_SCHEMA
]
});
TestBed.overrideProvider(PersonalSettingsService,{useValue: personalSettingsServiceMock});
TestBed.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(PersonalSettingsComponent);
component = fixture.componentInstance;
personalSettingsServiceMock.getPersonalSettings.and.returnValue(of(data));
fixture.detectChanges();
});
it('should get user data from userService', () => {
expect(component).toBeTruthy();
expect(component.personalData).toBe(data);
fixture.detectChanges();
})
});
app.component.ts Below is the method i need to test in component app.component.ts 下面是我需要在组件中测试的方法
getUserData () {
this._loaderEmitter.emitChange(APP_CONSTANTS.SHOW_LOADING);
this.personalSettingsService.getPersonalSettings().subscribe( (responseData:PERSONAL_DATA) => {
this._loaderEmitter.emitChange(APP_CONSTANTS.HIDE_LOADING);
this.personalData = responseData;
this.setSettingsData();
this.personalData = _.omit(this.personalData, 'email');
this.setChangeDetection();
});
}
Thanks in advance.提前致谢。
As it indicates getPersonalSettings
"real" implementation uses an Observable
that you subscribe to, so in your test you have to mock an Observable
因为它表明
getPersonalSettings
"real" 实现使用您订阅的Observable
,所以在您的测试中,您必须模拟一个Observable
import { of } from 'rxjs';
personalSettingsServiceMock = jasmine.createSpyObj('PersonalSettingsService', {
getPersonalSettings: of({})
});
and then you also need to add it to the providers this way:然后您还需要以这种方式将其添加到提供程序中:
{
provide: PersonalSettingsService,
useValue: personalSettingsServiceMock
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.