I believe in your test you have to call scope.$digest() in order for the
promise to be consumed (before your expect).


On Mon, May 12, 2014 at 2:15 PM, Diego Freitas <[email protected]> wrote:

> Hey guys, I'm facing a problem when trying to run and create my tests ...
>
> Here is my method's controller I'm trying to test:
>
>> $scope.findAll = function(){
>>         var promise = ContactService.findAll(); //promise that something
>> will be executed
>>         promise.then(
>>             function(contacts){
>>                 $scope.contacts = contacts;
>>             },
>>             function(reason){
>>                 alert('Failed: ' + reason);
>>             });
>>     }
>
>
> Here is my service's method I'll call:
>
>> this.findAll = function () {
>>       var deferred = $q.defer(); //deferred represents a task that will
>> be done in the future
>>
>> $http.get(config.BASE_URL + '/contacts')
>>       .success(function(data, status) {
>>       deferred.resolve(data);
>> })
>> .error(function(data) {
>> console.log('Error loading Contacts: ' + data);
>> deferred.reject(data);
>> });
>>       return deferred.promise;
>>     };
>
>
>  This is my script test I'm trying to run:
>
>> var myServiceMock, $httpBackend, ContactControllerMock,
>>         BASE_URL = "http://localhost:3000";;
>>
>>     beforeEach(inject(function ($controller, $rootScope){
>>         scope = $rootScope.$new();
>>         ContactControllerMock = $controller('ContactController', {
>>             $scope: scope
>>         });
>>     }));
>>
>>     it('should find all the contacts', inject( function (ContactService,
>> $q, $http, config){
>>         // Return a successful promise from the ContactService
>>         var deferredSuccess = $q.defer();
>>         spyOn(ContactService,
>> 'findAll').andReturn(deferredSuccess.promise);
>>         ContactService.findAll().then(
>>             function(contacts){
>>                 $scope.contacts = contacts;
>>             },
>>             function(reason){
>>                 alert('Failed: ' + reason);
>>             });
>>
>>         expect(ContactService.findAll).toHaveBeenCalled();
>>         deferredSuccess.resolve(); // resolves the promise
>>
>>         //expect(scope.contacts.length).toBeGreaterThan(0);
>>     }))
>
>
>
> Is not entering in the ServiceFindAll() function ... am I testing it
> right? What are the suggestion for the findAll test?
>
> Thanks all in advance!
>
>  --
> 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.

Reply via email to