[英]Angular 4, unit testing
I have one component which consists of one method which internally calls service and I need to do unit testing for the same. 我有一个组件,该组件由一种内部调用服务的方法组成,我需要对此进行单元测试。
Component.ts: Component.ts:
constructor(
private formUtils: MeasureFormService,
}
getList() {
this.formUtils.getList()
.subscribe((data) => {
this.memberDetails = data.MemberDetails;
this.chaserDetails = data.RelatedChaserDetails;
}, error => {
});
}
MeasureFormService.ts: MeasureFormService.ts:
getList() {
return this._httpWrapperService.Get('../../../../../assets/json/overread-accept.json')
.map((data: any) => {
return data;
});
}
I need to unit test the component method which calls service. 我需要对调用服务的组件方法进行单元测试。
Try this. 尝试这个。
describe('Test', () => {
let component: NotificationComponent;
let fixture: ComponentFixture<YourComponent>;
let measureFormService: MeasureFormService;
let mockMeasureFormService;
let listSubject;
beforeEach(async(() => {
listSubject = new Subject < any > ();
mockMeasureFormService = {
getList: () => {
return listSubject;
}
};
TestBed.configureTestingModule({
declarations: [YourComponent],
providers: [{provide: MeasureFormService, useValue: mockMeasureFormService}]
}).compileComponents();
fixture = TestBed.createComponent(YourComponent);
component = fixture.componentInstance;
notificationService = TestBed.get(NotificationService);
}));
it('should have a list of objects you need', () => {
component.getList();
listSubject.next(//just put a test object you are getting from the service);
fixture.detectChanges();
expect(component.memberDetails).toEqual(//service response);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.