我是新手测试。我有一个service
和一个spec
文件,运行时我收到以下错误:
错误:无法解析DashboardService的所有参数:(?)。错误属性:Object({ngSyntaxError:true})
spec
文件如下所示:
import { TestBed } from '@angular/core/testing';
import { DashboardService } from './dashboard.service';
import { ApiService } from './../api.service';
describe('The Dashboard Service', () => {
beforeEach(() => {
TestBed.configureTestingModule({ providers: [DashboardService, ApiService] });
});
it('should be created', () => {
const service: DashboardService = TestBed.get(DashboardService);
expect(service).toBeTruthy();
});
});
到目前为止,service
看起来像这样:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { ApiService } from './../api.service';
import { Organization } from '../../models/organization';
@Injectable({
providedIn: ApiService
})
export class DashboardService {
constructor(private api: ApiService) {}
getPrograms(id: number): Observable<any> {
let url = '/apiurl' + id;
return this.api.get<Organization>(url);
}
}
所以我猜错误是因为service
文件的依赖性,但在阅读Angular文档后,我仍然不确定如何让Angular知道这些依赖关系。如何构造spec
文件以正确读取依赖项?
这样的事情怎么样?
第一:
import { inject } from '@angular/core/testing';
然后:
it('should be created', inject([DashboardService], (dashboardService: DashboardService) => {
expect(dashboardService).toBeTruthy();
}));
我将import 'core-js/es7/reflect';
添加到test.ts
文件中。