簡體   English   中英

測試angular2服務

[英]Testing angular2 service

在下面的情況下,我收到以下錯誤,但我不知道為什么發生。 第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();
  });
});

我不知道為什么您的錯誤消息不能反映出來,但是Http有幾個依賴項。

代替Http添加

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM