Angular / jasmine / promise / mockery test

advertisements

I'm a little screwed up with angular / jasmine / promise / mocking testing.

I have a piece of code:

factory.login = function(user, pwd) {
    AuthenticationResource.login( {}, {"username" : user, "password" : pwd},
        factory.onLoginSuccess,
        factory.onLoginFailure
    );
};

Then I'm trying to write a test:

  it('login', function() {

        // give
        // 1. make 'authenticationResource.login' return a promise that will be successfully resolved
        var deferred = $q.defer();
        var promise = defered.promise;

        authenticationResource.login = function() {
            //deferred.resolve(loginResponse);
            return deferred.promise;
        };

        // 2. call 'factory.login' and make sure that 'onLoginSuccess' function was invoked as expected

        // then
        //expect(..
    });

Could you pointing me to right direction. Seems that all looks a bit different from what I've got to with Java/Mockito.?

Then AuthenticationResource itself is here:

angular.module('main.resources').factory('AuthenticationResource', ['$resource', function ($resource) {
    return $resource('/rest/authenticate/:path',
        {path: "@path"}, //parameters default
        {
            login:  { method: "POST", params: { path: "login" } },
            logout: { method: "POST", params: { path: "logout" } }
        });
}]);


Check my answer here

Mocking promises is difficult until u see the light!