Angular Testing Error: Can't resolve all parameters for Service:

大城市里の小女人 提交于 2019-12-10 17:48:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!