使用依赖服务进行角度2单元测试

时间:2017-02-20 11:43:01

标签: unit-testing angular angular2-services

我是angular2的新手并试图为服务编写测试。我的应用程序中有一个用户服务,它是多个其他服务和参数的依赖服务。用户服务正在后端进行rest-api调用以获取用户信息。

我很难理解将依赖服务注入此用户服务的正确方法是什么,因此它可以进行http调用并获得响应。

用户service.ts

export class UserService extends HttpService {

private _authToken: string;
private _persistent: boolean;

constructor(http: Http, private sessionService: SessionService, private router: Router) {
    super(http);
    this._authToken = localStorage.getItem(AUTH_TOKEN_KEY);
}

get authToken(): string {
    return this._authToken;
}

会话service.ts

export class SessionService extends HttpService {

constructor(http: Http) {
    super(http);
    this.rootUrl = Constants.API_ROOT;
}

addSession(authToken: string, persistent:boolean=false): Observable<any> {
    return this.post(SESSIONS_ROOT, {persistent: persistent}, TokenService.AuthHeader(authToken));
}

用户service.spec.ts

 describe(' #######  User Service Test Cases  ####### ', () =>  beforeEach(() => {
TestBed.configureTestingModule({
  providers: [
    // {
    //   provide: Router, 
    //   useClass: class { navigate = jasmine.createSpy("navigate")},  
    // },
    BaseRequestOptions,
    MockBackend,
    {
      provide: Http,
      useFactory: (backend: MockBackend, defaultOptions: BaseRequestOptions) => {
        return new Http(backend, defaultOptions);
      },
      deps: [MockBackend, BaseRequestOptions],
    },
    SessionService,
    UserService
  ],
  imports: [RouterTestingModule]
});   });   it('test method', inject([UserService], (userService: UserService) => {
    expect(userService.isEmailAvailable('a@a.com')).toBe('true');
 }));

});

但是,我在执行时遇到错误。

  

#######用户服务测试用例#######       ×应该在电子邮件已被包含时返回响应           预期的AnonymousSubject({_ isScalar:false,observers:[],closed:false,isStopped:false,hasError:false,thrownError:null,destination:AnonymousSubject({_ isScalar:false,observers:[],closed:false,isStopped:false ,hasError:false,thrownError:null,destination:AnonymousSubject({_ isScalar:false,observers:[],closed:false,isStopped:false,hasError:false,thrownError:null,destination:ReplaySubject({_ isScalar:false,observers: [],closed:false,isStopped:false,hasError:false,thrownError:null,scheduler:undefined,_events:[],_ _ bufferSize:1,_windowTime:Infinity}),source:ReplaySubject({_ isScalar:false,observers:[ ],closed:false,isStopped:false,hasError:false,thrownError:null,scheduler:undefined,_events:[],_ bufferSize:1,_windowTime:Infinity}),operator:TakeOperator({total:1})}), source:AnonymousSubject({_ isScalar:false,observers:[],closed:false,isStopped:false,hasError:false,thrownError:null,desti nation:ReplaySubject({_ isScalar:false,observers:[],closed:false,isStopped:false,hasError:false,thrownError:null,scheduler:undefined,_events:[],_ blufferSize:1,_windowTime:Infinity}),source :ReplaySubject({_ isScalar:false,observers:[],closed:false,isStopped:false,hasError:false,thrownError:null,scheduler:undefined,_events:[],_ blufferSize:1,_windowTime:Infinity}),operator: TakeOperator({total:1})}),运算符:MapOperator({project:Function,thisArg:undefined})}),source:AnonymousSubject({_ isScalar:false,observers:[],closed:false,isStopped:false, hasError:false,thrownError:null,destination:AnonymousSubject({_ isScalar:false,observers:[],closed:false,isStopped:false,hasError:false,thrownError:null,destination:ReplaySubject({_ isScalar:false,observers:[ ],closed:false,isStopped:false,hasError:false,thrownError:null,scheduler:undefined,_events:[],_ blufferSize:1,_windowTime:Infinity} ),source:ReplaySubject({_ isScalar:false,observers:[],closed:false,isStopped:false,hasError:false,thrownError:null,scheduler:undefined,_events:[],_ blufferSize:1,_windowTime:Infinity}) ,operator:TakeOperator({total:1})}),source:AnonymousSubject({_ isScalar:false,observers:[],closed:false,isStopped:false,hasError:false,thrownError:null,destination:ReplaySubject({_ isScalar :false,观察者:[],关闭:false,isStopped:false,hasError:false,thrownError:null,scheduler:undefined,_events:[],_ cacheStize:1,_windowTime:Infinity}),source:ReplaySubject({_ isScalar: false,观察者:[],关闭:false,isStopped:false,hasError:false,thrownError:null,scheduler:undefined,_events:[],_ buffuffSize:1,_windowTime:Infinity}),operator:TakeOperator({total:1 })},operator:MapOperator({project:Function,thisArg:undefined})}),operator:CatchOperator({selector:Function,caught:})})为'true'。               在webpack:///src/app/test/user.spec.ts:75:69&lt; - config / karma-test-shim.js:87887:71 [ProxyZone]

我不确定从错误中理解什么。有关如何正确处理或注入依赖服务以及创建实例的任何帮助都将有所帮助。 非常感谢。感谢帮助。

0 个答案:

没有答案