1

In the scenario below, I am getting the following error which I have no clue why this is occurring. Line 71 is the expect test failing. Any help deciphering what the issue could be would be greatly appreciated!

PhantomJS 2.1.1 (Mac OS X 0.0.0) UserService should be able to create a user service FAILED
        _instantiateProvider@/Users/nsmith/projects/cohortable/spec-bundle.js:12308:38 <- webpack:///angular2/src/core/di/injector.ts:769:31
        _new@/Users/nsmith/projects/cohortable/spec-bundle.js:12297:42 <- webpack:///angular2/src/core/di/injector.ts:758:37
        getObjByKeyId@/Users/nsmith/projects/cohortable/spec-bundle.js:11907:55 <- webpack:///angular2/src/core/di/injector.ts:355:44
        _getByKeyDefault@/Users/nsmith/projects/cohortable/spec-bundle.js:12493:51 <- webpack:///angular2/src/core/di/injector.ts:973:44
        _getByKey@/Users/nsmith/projects/cohortable/spec-bundle.js:12439:42 <- webpack:///angular2/src/core/di/injector.ts:910:35
        _getByDependency@/Users/nsmith/projects/cohortable/spec-bundle.js:12425:35 <- webpack:///angular2/src/core/di/injector.ts:892:28
        _instantiate@/Users/nsmith/projects/cohortable/spec-bundle.js:12317:53 <- webpack:///angular2/src/core/di/injector.ts:781:46
        _instantiateProvider@/Users/nsmith/projects/cohortable/spec-bundle.js:12308:38 <- webpack:///angular2/src/core/di/injector.ts:769:31
        _new@/Users/nsmith/projects/cohortable/spec-bundle.js:12297:42 <- webpack:///angular2/src/core/di/injector.ts:758:37
        getObjByKeyId@/Users/nsmith/projects/cohortable/spec-bundle.js:11907:55 <- webpack:///angular2/src/core/di/injector.ts:355:44
        _getByKeyDefault@/Users/nsmith/projects/cohortable/spec-bundle.js:12493:51 <- webpack:///angular2/src/core/di/injector.ts:973:44
        _getByKey@/Users/nsmith/projects/cohortable/spec-bundle.js:12439:42 <- webpack:///angular2/src/core/di/injector.ts:910:35
        _getByDependency@/Users/nsmith/projects/cohortable/spec-bundle.js:12425:35 <- webpack:///angular2/src/core/di/injector.ts:892:28
        _instantiate@/Users/nsmith/projects/cohortable/spec-bundle.js:12317:53 <- webpack:///angular2/src/core/di/injector.ts:781:46
        _instantiateProvider@/Users/nsmith/projects/cohortable/spec-bundle.js:12308:38 <- webpack:///angular2/src/core/di/injector.ts:769:31
        _new@/Users/nsmith/projects/cohortable/spec-bundle.js:12297:42 <- webpack:///angular2/src/core/di/injector.ts:758:37
        getObjByKeyId@/Users/nsmith/projects/cohortable/spec-bundle.js:11907:55 <- webpack:///angular2/src/core/di/injector.ts:355:44
        _getByKeyDefault@/Users/nsmith/projects/cohortable/spec-bundle.js:12493:51 <- webpack:///angular2/src/core/di/injector.ts:973:44
        _getByKey@/Users/nsmith/projects/cohortable/spec-bundle.js:12439:42 <- webpack:///angular2/src/core/di/injector.ts:910:35
        _getByDependency@/Users/nsmith/projects/cohortable/spec-bundle.js:12425:35 <- webpack:///angular2/src/core/di/injector.ts:892:28
        _instantiate@/Users/nsmith/projects/cohortable/spec-bundle.js:12319:53 <- webpack:///angular2/src/core/di/injector.ts:783:46
        _instantiateProvider@/Users/nsmith/projects/cohortable/spec-bundle.js:12308:38 <- webpack:///angular2/src/core/di/injector.ts:769:31
        _new@/Users/nsmith/projects/cohortable/spec-bundle.js:12297:42 <- webpack:///angular2/src/core/di/injector.ts:758:37
        getObjByKeyId@/Users/nsmith/projects/cohortable/spec-bundle.js:11907:55 <- webpack:///angular2/src/core/di/injector.ts:355:44
        _getByKeyDefault@/Users/nsmith/projects/cohortable/spec-bundle.js:12493:51 <- webpack:///angular2/src/core/di/injector.ts:973:44
        _getByKey@/Users/nsmith/projects/cohortable/spec-bundle.js:12439:42 <- webpack:///angular2/src/core/di/injector.ts:910:35
        get@/Users/nsmith/projects/cohortable/spec-bundle.js:12116:31 <- webpack:///angular2/src/core/di/injector.ts:576:26
        /Users/nsmith/projects/cohortable/spec-bundle.js:28333:36 <- webpack:///src/app/services/UserService.spec.ts:52:31
        _instantiate@/Users/nsmith/projects/cohortable/spec-bundle.js:12413:87 <- webpack:///angular2/src/core/di/injector.ts:879:67
        Expected undefined to be defined.
        /Users/nsmith/projects/cohortable/spec-bundle.js:28336:41 <- webpack:///src/app/services/UserService.spec.ts:71:35

I have a basic UserService that takes a couple of objects in the constructor and if I want to test. I believe I have included everything that it needs for the test.

import { Store } from '@ngrx/store';
import { Router } from 'angular2/router';
import { Http } from 'angular2/http';

export const user: Reducer<any> = (state:any = initialState, action:Action) => {
  ...
}

@Injectable()
export class UserService {
  constructor(public http:Http, private store:Store<any>, private router:Router) {}
}

export var USER_SERVICE_PROVIDERS: Array<any> = [
  ROUTER_PROVIDERS,
  Http,
  provideStore({user}),
  UserService,
];

-

import { UserService } from './UserService';
import { Injector, provide } from 'angular2/core';

describe('UserService', () => {
  let injector:Injector;
  let userService:UserService;

  beforeEach(() => {
    injector = Injector.resolveAndCreate([
        provide(APP_BASE_HREF, { useValue: '/' }),
        provide(ApplicationRef, {useClass: MockApplicationRef}),
        provide(XHRBackend, {useClass: MockBackend}),
        USER_SERVICE_PROVIDERS
    });
    userService = injector.get(UserService);
  });

  it('should be able to create a user service', () => {
    expect(userService).toBeDefined();
  });
});
nathasm
  • 2,524
  • 5
  • 24
  • 35

1 Answers1

2

I wonder why your error message doesn't reflect that, but Http has several dependencies.

Instead of Http add

HTTP_PROVIDERS, MockBackend, provide(XHRBackend, {useExisting: MockBackend}) 
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Thanks for the quick response, however that didn't resolve the issue. It *did* however prompt me to comment out everything and basically start from scratch. The culprit right now is that the router being injected in to the UserService is causing the error, not the Http component. So it has to be something not being injected that the Router component needs. – nathasm Feb 16 '16 at 05:45
  • 1
    Yeah, saw that. Didn't help much even after adding ROUTER_PRIMARY_COMPONENT (possibly because this is a service and not a component). Might be time to refactor the router out of the service instead. Appreciate the help. I'll leave the question open but will mark as answered in a couple of days. – nathasm Feb 16 '16 at 06:01