mardi 25 août 2015

Testing Rest Calls with karma and angular

I'm trying to test my angular rest calls with karma and jasmine. for starters I just want to test my Auth Service and do a simple login.

describe('Auth Service', function () {
    var httpBackend,
        service;

    beforeEach(function () {
        angular.mock.module('mean');

        inject(function ($httpBackend, Auth) {
            httpBackend = $httpBackend;
            httpBackend.expect('POST', '/auth/v1/login', { email: 'my@email.com', password : 'password' }).respond({
                response: {
                    success: true
                }
            });

            service = Auth;
        });
    });

    it('should login.', function () {
        var result;

        service.login('my@email.com', 'password').then(function (response) {
            result = response;
        });

        httpBackend.flush();
        // TODO
    });
});

I know that this code won't work as is, but unfortunately I don't even get as far as to check if the rest call is being executed properly. I get the following exception:

Error: Unexpected request: GET internationalization/locale-en.json
Expected POST /auth/login

locale-en.json is one of my internationalization files which I didn't reference anywhere in this Test (or even in the Auth Service).

here's the implementation of the login function in the Auth Service:

function login(email, password) {
    var deferred = $q.defer();
    $http.post('/auth/v1/login', {email: email, password: password})
        .success(function (t) {
            $localStorage.token = t;
            deferred.resolve(t);
        })
        .error(function (err) {
            deferred.reject(err);
        });

    return deferred.promise;
}

Where does the seemingly random GET call to the json come from and what am I doing wrong here?


versions: angular 1.4.4 karma 0.13.9 jasmine 2.3.4

Aucun commentaire:

Enregistrer un commentaire