2013-10-09 8 views
5

मैं कुछ संसाधनों का उपयोग करने वाली एक सेवा का उपयोग कर एक निर्देश का परीक्षण करने की कोशिश कर रहा हूं। मेरे पास यह मुद्दा है जब मैं अपने संसाधन की get विधि का नकल करता हूं तो इसे मजाक कर दिया जाएगा लेकिन कॉलबैक फ़ंक्शन नहीं कहा जाएगा। इसलिए, परिणाम अपेक्षित नहीं होगा।एंगुलरजेएस, यूनिट एक ऐसे निर्देश का परीक्षण करता है जो किसी सेवा का उपयोग करता है जो कुछ संसाधनों पर निर्भर करता है

मैंने spyOn का उपयोग करके here का उपयोग करके संसाधनों का नकल करने की कोशिश की, और $httpBackend.when, लेकिन न तो काम किया। जब मैं कोड डीबग करता हूं तो यह विधि प्राप्त करने के लिए मिलता है लेकिन कॉलबैक फ़ंक्शन कभी कॉल नहीं होता है और इसलिए, आंतरिक कॉलबैक myCallback जो मेरा मान सेट करता है उसे कभी भी कॉल नहीं किया जाता है। मुझे यकीन नहीं है कि मेरा दृष्टिकोण भी सही है, मैं आपके सुझावों की सराहना करता हूं।

/संसाधन

.factory ('AirportTimeZone', function($resource){ 
    return $resource('/api/airport/:airportId/timezone',{airportId: '@airportId'}); 
}) 

/कि मेरी संसाधनों का उपयोग कर रहा है सेवा:

angular.module('localizationService', []) 
.factory('LocalizationService', ['AirportTimeZone','CurrentLocalization', 
    function (AirportTimeZone,CurrentLocalization) { 

    function getAirportTimeZone(airport,myCallback){ 
     var options = {} 
     var localOptions = AirportTimeZone.get({airportId:airport}, function(data){ 
      options.timeZone = data.timeZoneCode 
      myCallback(options) 
     }); 
    } 
}) 

/निर्देशक

.directive('date',function (LocalizationService) { 
    return function(scope, element, attrs) { 
     var airTimeZone 
     function updateAirportTimeZone(_airportTimeZone){ 
      airTimeZone = _airportTimeZone.timeZone 
      // call other stuff to do here 
     } 
     .... 
     LocalizationService.getAirportTimeZone(airport,updateAirportTimeZone) 
     .... 
     element.text("something"); 
    } 
}); 

/टेस्ट

describe('Testing date directive', function() { 
    var $scope, $compile; 
    var $httpBackend,airportTimeZone,currentLocalization 

    beforeEach(function(){ 
     module('directives'); 
     module('localizationService'); 
     module('resourcesService'); 
    }); 

    beforeEach(inject(function (_$rootScope_, _$compile_,AirportTimeZone,CurrentLocalization) { 
     $scope = _$rootScope_; 
     $compile = _$compile_; 
     airportTimeZone=AirportTimeZone; 
     currentLocalization = CurrentLocalization; 

//  spyOn(airportTimeZone, 'get').andCallThrough(); 
//  spyOn(currentLocalization, 'get').andCallThrough(); 

    })); 

    beforeEach(inject(function($injector) { 
     $httpBackend = $injector.get('$httpBackend'); 
//  $httpBackend.when('GET', '/api/timezone').respond({timeZone:'America/New_York',locale:'us-en'}); 
//  $httpBackend.when('GET', '/api/airport/CMH/timezone').respond({timeZone:'America/New_York'}); 
    })) 

    describe('Date directive', function() { 
     var compileButton = function (markup, scope) { 
      var el = $compile(markup)(scope); 
      scope.$digest(); 
      return el; 
     }; 

     it('should',function() { 
      var html = "<span date tz='airport' format='short' airport='CMH' >'2013-09-29T10:40Z'</span>" 
      var element = compileButton(html,$scope) 
      $scope.$digest(); 

      expected = "...." 
      expect(element.html()).toBe(expected); 


     }); 
    }); 
}) 
+1

सिर्फ लिखने की त्रुटियों हो सकता है, लेकिन अपने LocalizationService वैध जे एस की तरह नहीं दिखता (बेमेल []), और आप से कुछ भी वापस नहीं कर रहे हैं कारखाना, जिसका मतलब है कि सेवा अपरिभाषित के रूप में बाहर आ जाएगी। क्या आप अपने संकलन के बाद '$ httpBackend.flush() 'को कॉल करना भी याद कर रहे हैं? – Andyrooger

उत्तर

4

जैसा कि ऊपर टिप्पणी की:

$httpBackend.when साथ प्रतिक्रियाओं स्थापित करने के बाद, आप अभी भी मज़ाक उड़ाया प्रतिक्रिया बाहर फ्लश करने के लिए $httpBackend.flush() कॉल करने के लिए की आवश्यकता होगी।

संदर्भ:। http://docs.angularjs.org/api/ngMock $ httpBackend - फ्लशिंग HTTP अनुरोध अनुभाग

संबंधित मुद्दे

 संबंधित मुद्दे