Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm testing my directive which used Restangular for some API calls in the controller.

For functions with promises used by Restangular I'm mocking those functions and putting together with my main model, which looks like this:

    model = {
        comments: comments,

        one: function () {
            deferred = $q.defer();

            return deferred.promise;
        },
        post: function () {
            deferred = $q.defer();

            return deferred.promise;
        },
        remove: function () {
            deferred = $q.defer();

            return deferred.promise;
        }
    };

And so when executing a function which runs Restangular's function e.g.:

    $scope.model.post('comments', newComment).then(...);

Me myself deciding to reject or to resolve it:

    deferred.reject();
    $scope.$digest();

Everything works fine but not when the functions are nested, for example like here:

    $scope.model.one('comments', comment.id).remove().then(...);

In this case I'm getting an error that remove is not defined (though I defined it):

    TypeError: 'undefined' is not a function (evaluating '$scope.model.one('comments', comment.id).remove()')

Am I missing something?

share|improve this question
    
Whats the status of this? Can I help? –  apairet Aug 28 '14 at 22:21
    
If you can. I just skipped tests with nested actions, so .one.remove or .one.getList for example don't how how to mock... –  Julius Aug 29 '14 at 9:07

1 Answer 1

Your function 'one' returns a promise and not the model itself. The function 'remove' on the model but not on the promise. You can chain promise using 'then', but you can not chain your model function.

This code:

$scope.model.one('comments', comment.id).remove().then(...);

is equivalent to:

var promise = $scope.model.one('comments', comment.id);
promise.remove(); // error: 'remove' is undefined!

Another problem with your code is that you define deferred objects, and return promises (which is good), but you never 'resolve' or 'reject' your promise by calling 'resolve' or 'reject' functions of the deferred objects.

share|improve this answer
    
I think OP would call the 'resolve' or 'reject' in each test case, not in the mock object itself. –  runTarm Aug 19 '14 at 17:17
    
What do you mean by OP? –  apairet Aug 19 '14 at 17:18
    
"Original Poster", see: What does OP mean? –  runTarm Aug 19 '14 at 17:22
    
So how to add that remove function properly on my mock? –  Julius Aug 19 '14 at 17:33
    
Could you provide a complete example through jsfiddle or plunkr? Thx –  apairet Aug 19 '14 at 18:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.