我有一个调用服务的组件。我试图首先创建一个测试,以查看组件是否实际创建。我试图模拟服务类,但不断收到错误。我一直得到:"捕获不是一个功能"。我添加了所有可能的参考资料:
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
import { Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
这是我的代码:
来自我的组件:
ngOnInit():void{
this.bindCustomerId();
this.bindEmploymentDetails();
}
private bindEmploymentDetails(){
this.reviewService.getEmploymentDetails(this.reviewId)
.catch(this.handleError)
.subscribe(
(employmentDetails: EmploymentInfo[]) =>
{
this.employmentDetails = employmentDetails;
this.bindEmploymentDetailsVisibility(this.employmentDetails);
}
);
}
我的测试:
describe('EmploymentDetailsComponent', () => {
let component: EmploymentDetailsComponent;
let fixture: ComponentFixture<EmploymentDetailsComponent>;
let config: Route[] = [];
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
EmploymentDetailsModule,
RouterTestingModule.withRoutes(config),
FormsModule,
BrowserModule,
HttpModule,
],
declarations: [ ],
providers:[
{
provide:CustomerService,
useClass: mockCustomerService
},
{
provide: ReviewService,
useClass: ReviewServiceMock
},
{
provide: LookUpService,
useClass: mockCustomerService
}
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(EmploymentDetailsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
我使用所有相同的方法为ReviewServiceMock创建了一个模拟类。
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import { Injectable } from '@angular/core';
import { Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable()
export class ReviewServiceMock implements IReviewService{
private apiUrl: string;
public createNewReview(customerId: number): Observable<ReviewResponse>{
return null;
}
public getPersonalInfo(reviewId: number): Observable<PersonalInfo[]>{
return null;
}
public getPersonalInfoForSingleCustomer(reviewId: number, customerId: number): Observable<PersonalInfo>{
return null;
}
public getContactInfo(reviewId: number) : Observable<ContactInformation[]>{
return null;
}
public getContactInfoForSingleCustomer(reviewId: number, customerId : number) : Observable<ContactInformation>{
return null;
}
public saveContactInfo(contactInfo: ContactInformation, customerId : number, reviewId: number): Observable<any>{
return null;
}
public savePersonalInfo(personalInfo: PersonalInfo, customerId : number, reviewId: number){
return null;
}
public getEmploymentDetails(reviewId : number):Observable<EmploymentInfo[]> {
var mm = JSON.parse(
`[
{
"CustomerId": 1,
"EmployerName": "TMG",
"EmployerAddress": "Mosley road",
"EmploymentStartDate": "2016-12-28T13:49:36.317Z",
"EmploymentType": {
"Id": 1,
"Description": "Programmer"
},
"OccupationType": {
"Id": 0,
"Description": "string"
},
"JobTitle": {
"Id": 0,
"Description": "string"
},
"EstimatedMonthlyIncome": 0,
"CustomerPayingNationalInsurance": true,
"CustomerWorkingOver16HoursAWeek": true,
"AffectedByInsolvency": true
}
]` );
}
public getEmploymentDetailsForSingleCustomer(reviewId : number, customerId : number):Observable<EmploymentInfo>{
return null;
}
public saveEmploymentDetails(employmentDetails: EmploymentInfo, reviewId: number){
return null;
}
public confirmEmploymentDetails(reviewId: number, customerId: number){
return null;
}
public saveCustomerDependant(customerDependant:CustomerDependant, reviewId:number){
return null;
}
public getCustomerDependant(reviewId: number, dependantId:number){
return null;
}
public getAllDependants(reviewId: number){
return null;
}
public catch()
{
}
public deleteDependant(reviewId: number, dependantId:number){
return null;
}
public getHomeOwnerAndTenantInfoForReview(reviewId: number){
return null;
}
public saveHousingInfoMultipleCustomer(reviewId:number,housingInfo:any){
return null;
}
public getCurrentReview(customerId: number): Observable<ReviewResponse>{
return null;
}
}
答案 0 :(得分:4)
您需要实现ReviewServiceMock类。让getEmploymentDetails
和所有其他方法返回一个空的可观察者。 - &GT; return Observable.of([]);
问题是getEmploymentDetails
返回null而catch
确实不是null上的函数