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?

3 Answers 3

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();
}));
Sign up to request clarification or add additional context in comments.

1 Comment

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....
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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.