无法使用 jasmine 在 angular 中测试服务方法
Unable to test a service method in angular using jasmine
我正在 angular 中进行单元测试。在 component.ts
中,在 ngOnInit
中,我有一个服务方法调用,如下所示:
ngOnInit(){
this.DomainManagementService.getVerifiedDomains().then(res => {
this.claimedDomains = res.filter(domain => {
return domain.status === 'claimed';
});
});
}
我从 getVerifiedDomains()
方法获取数据,然后应用过滤方法仅过滤掉已声明的域。最后,我将过滤后的域存储在 this.claimedDomains
中。
现在我需要测试 this.claimedDomains
的长度是否大于 0。这该怎么做?请帮我写测试用例。
你必须先模拟 domainMainagementService
,我会使用 jasmine.createSpyObj
。
....
let mockDomainManagementService: jasmine.SpyObj<DomainManagementService>;
....
beforeEach(waitForAsync(() => {
mockDomainManagementService = jasmine.createSpyObj<DomainManagementService>('DomainManagementService', ['getVerifiedDomains']);
TestBed.configureTestingModule({
....
providers: [{ provide: DomainManagementService, useValue: mockDomainManagementService }],
}).compileComponents();
// Make sure this is there before the first fixture.detectChanges().
// The first fixture.detectChanges() calls ngOnInit.
// Mock the return value here.
mockDomainManagementService.getVerifiedDomains.and.returnValue(Promise.resolve([
{ status: 'claimed' },
]))
}));
it('should set claimedDomains', async () => {
// wait until all promises are resolved with fixture.whenStable()
await fixture.whenStable();
expect(component.claimedDomains.length).toBe(1);
});
您需要这样做来模拟您的服务调用:
// This will create a function that will inject the service in your test
function setup() {
const domainManagementService = fixture.debugElement.injector.get(DomainManagementService);
return { domainManagementService };
}
// - This is your test scenario
it('Load Claimed Domains ', () => {
const { domainManagementService} = setup();
spyOn(domainManagementService, 'getVerifiedDomains').and.returnValue(Promise.resolve([{status: 'claimed'},{status: 'claimed'}]));
component.ngOnInit();
expect(domainManagementService.getVerifiedDomains).toHaveBeenCalled();
expect(component.claimedDomains.length).toBe(2);
});
但请记住正确模拟您的服务响应,以便您拥有正确的“声明的域”。
我正在 angular 中进行单元测试。在 component.ts
中,在 ngOnInit
中,我有一个服务方法调用,如下所示:
ngOnInit(){
this.DomainManagementService.getVerifiedDomains().then(res => {
this.claimedDomains = res.filter(domain => {
return domain.status === 'claimed';
});
});
}
我从 getVerifiedDomains()
方法获取数据,然后应用过滤方法仅过滤掉已声明的域。最后,我将过滤后的域存储在 this.claimedDomains
中。
现在我需要测试 this.claimedDomains
的长度是否大于 0。这该怎么做?请帮我写测试用例。
你必须先模拟 domainMainagementService
,我会使用 jasmine.createSpyObj
。
....
let mockDomainManagementService: jasmine.SpyObj<DomainManagementService>;
....
beforeEach(waitForAsync(() => {
mockDomainManagementService = jasmine.createSpyObj<DomainManagementService>('DomainManagementService', ['getVerifiedDomains']);
TestBed.configureTestingModule({
....
providers: [{ provide: DomainManagementService, useValue: mockDomainManagementService }],
}).compileComponents();
// Make sure this is there before the first fixture.detectChanges().
// The first fixture.detectChanges() calls ngOnInit.
// Mock the return value here.
mockDomainManagementService.getVerifiedDomains.and.returnValue(Promise.resolve([
{ status: 'claimed' },
]))
}));
it('should set claimedDomains', async () => {
// wait until all promises are resolved with fixture.whenStable()
await fixture.whenStable();
expect(component.claimedDomains.length).toBe(1);
});
您需要这样做来模拟您的服务调用:
// This will create a function that will inject the service in your test
function setup() {
const domainManagementService = fixture.debugElement.injector.get(DomainManagementService);
return { domainManagementService };
}
// - This is your test scenario
it('Load Claimed Domains ', () => {
const { domainManagementService} = setup();
spyOn(domainManagementService, 'getVerifiedDomains').and.returnValue(Promise.resolve([{status: 'claimed'},{status: 'claimed'}]));
component.ngOnInit();
expect(domainManagementService.getVerifiedDomains).toHaveBeenCalled();
expect(component.claimedDomains.length).toBe(2);
});
但请记住正确模拟您的服务响应,以便您拥有正确的“声明的域”。