`ng test` shows Error: Can't resolve all parameters for BackendService

Below error shown when I ran ng test command.

Here is my service spec,

describe('BackendService', () => {
  beforeEach(() => {
      providers: [
          provide: Http, useFactory: (backend, options) => {
            return new Http(backend, options);
          deps: [MockBackend, BaseRequestOptions]

  it('should ...', inject([BackendService, MockBackend], (service: BackendService) => {

); });

BackendService.ts looks like,

export class BackendService {
  private baseUrl: string = 'https://foo-backend.appspot.com/_ah/api/default/v1';

  constructor(private http: Http, baseName: string) {
    this.baseUrl = this.baseUrl + baseName;

It seems like extra parameter inside the BackendService class's constructor causes this problem..


How do you expect Angular to know what baseName is supposed to be? All constructor parameters need to be obtained from the Injector. And if there is no corresponding token for the parameter, then it can't be looked up.

You can add a token by doing

// somewhere in some file
import { OpaqueToken } from '@angular/core';

export const BASE_NAME_TOKEN = new OpaqueToken("app.base_name");

// in test class
import { BASE_NAME_TOKEN } from 'where-ever'

  providers: [
    { provide: BASE_NAME_TOKEN, useValue: 'whatever-the-base-is' }

// in service constructor
import { Inject } from '@angular/core'
import { BASE_NAME_TOKEN } from 'where-ever'

constructor(http: Http, @Inject(BASE_NAME_TOKEN) baseName: string) {}

See Also:


 ? What's better to use in Angular2 - src or [src]
 ? Creating and publishing an NPM module with ES6 collection types
 ? How to pass data and image both to "ASP.NET Core" Web API using Angular 2(typescript)?
 ? Using *ngIf causes infinite loop with HTTP request
 ? Return type of a Function which returns a Function
 ? Missing onresourcetimingbufferfull event from Performance interface
 ? Limit what Type’s can be passed to method through interface
 ? toString,hashcode and equals method inside interface
 ? How can I inject interface into controller's constructor in Yii2
 ? How to use FileNameMap interface in Java?