2013-03-24 11 views
14

Ho un semplice servizio che sto cercando di testare unitamente. Non importa quello che provo, o searchService è un provider sconosciuto, o il servizio è nullo (che stranamente non farà fallire il mio test !!).Test di un semplice servizio AngularJS utilizzando Jasmine

Qualcuno ha un'idea di cosa potrei fare di sbagliato ??

angular.module('app').service('searchService', function($q, _) { // _ is lodash 

    var cache = [ 
    { 
     id: "current", 
     name: "Current", 
     description: "Search current data" 
    }, 
    { 
     id: "historical", 
     name: "Historical", 
     description: "Search historical data" 
    } 
    ]; 

    this.getSearchOptions = function() { 
    var deferred = $q.defer(); 
    deferred.resolve(angular.copy(cache)); 
    return(deferred.promise); 
    }; 

    this.getSearchOptionsByID = function(id) { 
    var deferred = $q.defer(); 
    var searchOption = _.findWithProperty(cache, "id", id); 

    if (searchOption) { 
     deferred.resolve(angular.copy(searchOption)); 
    } else { 
     deferred.reject(); 
    } 
    return(deferred.promise); 
    }; 
    } 
); 

Sto cercando di creare una prova di unità che carica nel searchService così posso controllare i valori memorizzati nella cache:

describe("Unit: Testing Services", function() { 
    describe("Search Service:", function() { 
    var service = null; 

    beforeEach(function() { 
     angular.module('app').service('_'); 
    }); 
    // I've also tried the commented out code below 
    //beforeEach(inject(function(searchService) { 
    //this.service = searchService; 
    //})); 
    //it('should contain an searchService', invoke(function(service) { 

    it('should contain an searchService', function(searchService) { 
     expect(searchService).not.to.equal(null); 
    }); 

    it('should contain two search options', function(searchService) { 
     expect(searchService.getSearchOptions()).to.equal(2); 
    }); 
    }); 
}); 

risposta

21

Il seguente dovrebbe funzionare per un servizio senza parametri, forse questo può essere un punto di partenza:

describe("Unit: Testing Services", function() { 
    describe("Search Service:", function() { 

     beforeEach(function() { 
      angular.module('app'); 
     }); 

     it('should contain a searchService', 
      inject(function(searchService) { 
       expect(searchService).not.to.equal(null); 
     })); 

     it('should contain two search options', 
      inject(function(searchService) { 
       expect(searchService.getSearchOptions()).to.equal(2); 
     })); 

    }); 
}); 

(si può anche avere uno sguardo qui: How do I test an AngularJS service with Jasmine?)

+1

ha ottenuto io vado nella giusta direzione. Grazie! Ho lasciato cadere angular.module ('app') e ho appena fatto il modulo ('app'). E poi cambiato invocare per iniettare (era tardi, suppongo che le mie dita avessero una mente propria) – nathasm

+5

Bello da sentire. Come nota a margine, in realtà module ('app') è una scorciatoia per angular.mock.module ('app') che ti permette di creare mock del tuo vero modulo. – Flolagale

Problemi correlati