测试angular2服务

时间:2016-02-16 05:20:37

标签: angular

在下面的场景中,我收到以下错误,我不知道为什么会发生这种情况。第71行是期望测试失败。任何有助于破译问题的帮助都将不胜感激!

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

我有一个基本的UserService,在构造函数中需要几个对象,如果我想测试的话。我相信我已经包含了测试所需的一切。

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();
  });
});

1 个答案:

答案 0 :(得分:2)

我想知道为什么你的错误信息没有反映出来,但是Http有几个依赖关系。

而不是Http添加

HTTP_PROVIDERS, MockBackend, provide(XHRBackend, {useExisting: MockBackend})