From ffb720757d7e25113e965115c77297ef9db9491f Mon Sep 17 00:00:00 2001 From: Braun Patrik Date: Wed, 4 May 2016 22:37:26 +0200 Subject: [PATCH] implementing userService test --- .../app/model/network/user.service.spec.ts | 50 +++++++++++++++++++ frontend/app/model/network/user.service.ts | 9 ++-- 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 frontend/app/model/network/user.service.spec.ts diff --git a/frontend/app/model/network/user.service.spec.ts b/frontend/app/model/network/user.service.spec.ts new file mode 100644 index 00000000..d4b2b072 --- /dev/null +++ b/frontend/app/model/network/user.service.spec.ts @@ -0,0 +1,50 @@ +/// + +import {it, inject, beforeEachProviders, beforeEach, afterEach} from "@angular/core/testing"; +import {BaseRequestOptions, Http, Response, ResponseOptions} from "@angular/http"; +import {MockBackend, MockConnection} from "@angular/http/testing"; +import {provide} from "@angular/core"; +import "rxjs/Rx"; +import {NetworkService} from "./network.service"; +import {Message} from "../../../../common/entities/Message"; +import {UserService} from "./user.service"; +import {LoginComponent} from "../../login/login.component"; +import {LoginCredential} from "../../../../common/entities/LoginCredential"; + + +describe('UserService', () => { + + + beforeEachProviders(() => [ + MockBackend, + BaseRequestOptions, + provide(Http, { + useFactory: (backend, options) => { + return new Http(backend, options); + }, deps: [MockBackend, BaseRequestOptions] + }), + NetworkService, + UserService + ]); + + + + + + it('should call postJson at login', inject([UserService,NetworkService], (userService,networkService) => { + spyOn(networkService,"postJson"); + let credential = new LoginCredential("name","pass"); + userService.login(credential); + expect(networkService.postJson.calls.argsFor(0)).toEqual(["/user/login",{"loginCredential": credential}]); + })); + + it('should call getJson at getSessionUser', inject([UserService,NetworkService], (userService,networkService) => { + spyOn(networkService,"getJson"); + let credential = new LoginCredential("name","pass"); + userService.login(credential); + })); + + + + +}); diff --git a/frontend/app/model/network/user.service.ts b/frontend/app/model/network/user.service.ts index c838873d..12315e9d 100644 --- a/frontend/app/model/network/user.service.ts +++ b/frontend/app/model/network/user.service.ts @@ -8,21 +8,20 @@ import {User} from "../../../../common/entities/User"; import {Message} from "../../../../common/entities/Message"; @Injectable() -export class UserService extends NetworkService{ +export class UserService{ - constructor(_http:Http){ - super(_http); + constructor(private _networkService:NetworkService){ } public login(credential:LoginCredential): Promise>{ - return this.postJson("/user/login",{"loginCredential": credential}); + return this._networkService.postJson("/user/login",{"loginCredential": credential}); } public getSessionUser(): Promise>{ - return this.getJson("/user/login"); + return this._networkService.getJson("/user/login"); } }