我正在为我的应用程序使用 angular2.0
我正在尝试为注入服务的组件编写测试用例
//组件.ts
import { Component, OnInit } from '@angular/core';
import { FPService } from './shared/services/forgot-password.service';
import { CookieService } from 'angular2-cookie/core';
@Component({
// moduleId: module.id,
selector: 'app-fp',
templateUrl: 'fp.component.html',
styleUrls: ['fp.component.css'],
providers: [FPService]
})
export class FPComponent implements OnInit {
constructor(
private cookie: CookieService,
private fpService: FPService
) { }
ngOnInit() {
}
fnRequest(email) {
var oData = { "email": email };
this.fpService.fnFPServie(oData)
.subscribe(
data => {
console.log('success', data);
},
error => {
console.log('error', error);
}
);
}
}
我的规格
import { FPComponent } from './forgot-password.component';
import { FPService } from './shared/services/forgot-password.service';
import { CookieService } from 'angular2-cookie/core';
class mockFPService {
fnFPServie(data) {
return {}
}
}
class mockCookieService {
getObject(name: string) {
return true;
}
}
let comp: FPComponent;
let fixture: ComponentFixture<FPComponent>;
describe('Component: FPComponent', () => {
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [FPComponent],
providers: [
{ provide: FPService, useClass: mockFPService },
{ provide: CookieService, useClass: mockCookieService },
]
});
fixture = TestBed.createComponent(FPComponent);
comp = fixture.componentInstance;
});
it('should have method fnRequest', () => {
expect(comp.fnRequest('adfa@fhf.fth')).toBeTruthy;
});
it('should sent data to fnFpService() of FpService', () => {
comp.fnRequest('asd@gmail.com');
**************************************************
how do inject the Fpservice and call fnFPServie ?
********************************************************
});
});
如果有人建议如何模拟注入的 Fpservice 来测试 fnRequest() 函数,那将会很有帮助
提前致谢