3

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?

Edgar Quintero
  • 4,223
  • 2
  • 34
  • 37

3 Answers3

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();
}));
Rene O
  • 11
  • 1
  • Hi @Rene O - Thanks! I ended up adding `import 'core-js/es7/reflect';` to the `test.ts` file and the error went away. Although I'm getting different probably non related errors now.... – Edgar Quintero Feb 12 '19 at 13:35
1

For me I was using TestBed to inject a regular class and got this error. This class was not injectable even though it was a service, we needed to have unique instances of it and that's what threw me off. Instead just create a new instance of this class using new testClass() etc. In my case I thought this class had an @Injectable() when it didn't

JesseBoyd
  • 1,022
  • 1
  • 13
  • 18
0

I added import 'core-js/es7/reflect'; to test.ts file.

Edgar Quintero
  • 4,223
  • 2
  • 34
  • 37