1
我使用angular2.0我的應用程序測試組件時,服務注入angular2.0
我試圖寫測試用例了業務的注入
//component.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);
}
);
}
}
我spec.ts
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('[email protected]')).toBeTruthy;
});
it('should sent data to fnFpService() of FpService',() => {
comp.fnRequest('[email protected]');
**************************************************
how do inject the Fpservice and call fnFPServie ?
********************************************************
});
});
這將是有益的,如果有人建議如何嘲笑注射Fpservice測試fnRequest()函數提前