mardi 2 février 2016

Testing calling of method on injected Service

Trying to figure out how check wether a method on an injected Service is being called.

The Service (based on Factory): loginService.js

angular.module('loginService', [])
    .factory('loginService', function (URLbase, $http, loginModel, $route, $location, logoutService, overviewService) {
        return {
            logMeIn: function (login) {

                $http({
                    method: 'POST',
                    url: URLbase + 'authentication/login',
                    data: {'Login': login.username, 'Password': login.password}
                }).then(function (result) {

                        loginModel.set('passwordRequested', false);
                        loginModel.set('loginFailed', false);
                        loginModel.set('loginExpired', false);
                        loginModel.set('loginBlocked', false);
                        loginModel.set('loggedIn', false);
                        loginModel.set('sessionExpired', false);
                        loginModel.set('loggedIn', false);
                        loginModel.set('concurrentLogin', false);
                        loginModel.set('somethingWrong', false);

                        // Check login result by status code
                        var status = result.data.Response.Status.StatusCode,
                            data = result.data.Response.Payload;

                    switch (status) {
                        case 0:
                            loginModel.set('tokenToken', data.Token);
                            loginModel.set('tokenUserId', data.userId);
                            loginModel.set('userName', data.User.FirstName + ' ' + data.User.SurName);
                            loginModel.set('userFirstName', data.User.FirstName);
                            loginModel.set('userLastName', data.User.SurName);
                            loginModel.set('userEmailAddress', data.User.Email);
                            loginModel.set('userPhone', data.User.Phone);
                            loginModel.set('userPreferredLanguage', data.User.PreferredLanguage);
                            loginModel.set('loggedIn', true);
        ......

The Unit test

beforeEach(inject(function ($controller, _$rootScope_, _$httpBackend_, $injector, _loginService_, _loginModel_) {
    loginService = _loginService_;
    httpBackend = _$httpBackend_;
    $rootScope = _$rootScope_;
    loginModel = _loginModel_;

    loginPost = {'Login': login.username, 'Password': login.password};
    httpBackend.whenPOST(URLbase + 'authentication/login', loginPost).respond(200, jsonObj);
}));

it('should get login data from the backend', function () {

    spyOn(loginService, 'logMeIn').andCallThrough();
    loginService.logMeIn(login);
    expect(loginService.logMeIn).toHaveBeenCalledWith(login);
    httpBackend.flush();

    //spyOn(loginModel, 'set').andCallThrough();
    //loginService >> logMeIn >> loginModel.set('userLastName', 'Vijver');
    //metod >> toHaveBeenCalledWith('userLastName', 'Vijver');
});

So in the last 3 commented lines I want to test if loginModel.set('userFirstName', data.User.FirstName); is being called with the correct paramaters.

How can I manage this?

Aucun commentaire:

Enregistrer un commentaire