2013-03-24 16 views
14

का उपयोग करके एक साधारण AngularJS सेवा का परीक्षण करना मेरे पास एक साधारण सेवा है जिसे मैं इकाई परीक्षण करने की कोशिश कर रहा हूं। इससे कोई फर्क नहीं पड़ता कि मैं क्या प्रयास करता हूं, या तो सर्च सर्विसेज एक अज्ञात प्रदाता है, या सेवा शून्य है (जो विचित्र रूप से पर्याप्त नहीं है कि मेरा परीक्षण विफल हो जाए !!)।जैस्मीन

किसी के पास कोई अंतर्दृष्टि है कि मैं क्या गलत कर रहा हूं ??

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

मुझे लगता है कि searchService में भार तो मैं कैश की गई मूल्यों की जाँच कर सकते हैं एक इकाई परीक्षण बनाने के लिए कोशिश कर रहा हूँ:

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

उत्तर

21

निम्नलिखित पैरामीटर के बिना एक सेवा के लिए काम करना चाहिए, हो सकता है कि यह एक हो सकता है प्रारंभिक बिंदु:

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

    }); 
}); 

(आप भी एक नज़र यहाँ हो सकता है: How do I test an AngularJS service with Jasmine?)

+1

यह मिल गया मैं सही दिशा में जा रहा हूँ। धन्यवाद! मैंने कोणीय। मॉड्यूल ('ऐप') गिरा दिया और बस मॉड्यूल ('ऐप') किया। और फिर इंजेक्ट करने के लिए आह्वान किया (यह देर हो चुकी थी, अनुमान लगाओ कि मेरी उंगलियों का अपना मन था) – nathasm

+5

सुनने के लिए अच्छा लगा। एक साइड नोट के रूप में, वास्तव में मॉड्यूल ('ऐप') angular.mock.module ('एप') के लिए एक शॉर्टकट है जो आपको अपने वास्तविक मॉड्यूल के मैक्स बनाने की अनुमति देता है। – Flolagale