首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试angular2服务

测试angular2服务
EN

Stack Overflow用户
提问于 2016-02-16 13:20:37
回答 1查看 210关注 0票数 1

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

代码语言:javascript
复制
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,它在构造函数中接受几个对象,如果我想测试的话。我相信我已经包含了测试所需的一切。

代码语言:javascript
复制
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,
];

-

代码语言:javascript
复制
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();
  });
});
EN

回答 1

Stack Overflow用户

发布于 2016-02-16 13:26:24

我想知道为什么您的错误消息没有反映这一点,但是Http有几个依赖项。

而不是Http add

代码语言:javascript
复制
HTTP_PROVIDERS, MockBackend, provide(XHRBackend, {useExisting: MockBackend}) 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35424368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档