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);
});
});
});
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
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