I see the code very bad from my Mobile phone, but first thing that comes into my mind is that if a piece of code is difficult to test then it should be refactored.
One thing that comes in to my mind is to use promise chaining instead of nesting. Sent from my iPhone > On 25/lug/2014, at 15:57, chris nikitas <[email protected]> wrote: > > Hi all, > > I am trying to test nested promised in my application without any luck so far. > > You can see my setup and code below: > > app.factory('myService', '$q', function($q) { > > var methodOne = function (param) { > > var deferred = $q.defer(); > > thirdParty.callMethodOne(param, function (myAPIError, success) { > if (_.isUndefined(success)) { > deferred.reject(myAPIError); > } > else { > deferred.resolve(success); > } > }); > > return deferred.promise; > > }; > > return { > > myPublicMethod: function(param) { > var deferred = $q.defer(); > > methodOne(param).then( > function(success) { > deferred.resolve('Yay'); > }, > function(myAPIError) { > deferred.reject('Nay'); > } > ) > } > > }; > > }); > > Trying to test the above: > > describe('Services', function () { > var service, $scope; > > beforeEach(function () { > angular.mock.module('services'); > }); > > beforeEach(angular.mock.inject(function(_$rootScope_) { > $scope = _$rootScope_.$new(); > })); > > describe('myService', function () { > > beforeEach(angular.mock.inject(function (myService) { > service = myService; > })); > > it('should be able to test my service', function() { > var param = 'test'; > var promise = service.myPublicMethod(param); > > promise.then(function(success) { > console.log('Yay'); > }, > function(error) { > console.log('Nay'); > } > > $scope.$apply(); > }); > > }); > > }); > > Any idea what might I might be doing wrong? During my test it will never go > into the then part (so nothing is printed to the console). > -- > You received this message because you are subscribed to the Google Groups > "AngularJS" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/angular. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "AngularJS" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/angular. For more options, visit https://groups.google.com/d/optout.
