问题
I'm new to testing. I have a service
and a spec
file that when run I get the following error:
Error: Can't resolve all parameters for DashboardService: (?). error properties: Object({ ngSyntaxError: true })
The spec
file looks like this:
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();
});
});
The service
looks like this so far:
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);
}
}
So I guess the error is because of the dependencies to the service
file but after reading the Angular documentation I'm still not sure of how to let Angular know about these dependencies. How do I structured the spec
file to read dependencies correctly?
回答1:
What about something like this?
First:
import { inject } from '@angular/core/testing';
then:
it('should be created', inject([DashboardService], (dashboardService: DashboardService) => {
expect(dashboardService).toBeTruthy();
}));
回答2:
I added import 'core-js/es7/reflect';
to test.ts
file.
来源:https://stackoverflow.com/questions/54639568/angular-testing-error-cant-resolve-all-parameters-for-service