2014-06-24 11 views
8

मैं एक तत्व के निर्देश है कि इसके link समारोह में एक सेवा कॉल बना रहा हूं:AngularJS निर्देश लिंक समारोह चमेली की परीक्षा में नहीं बुलाया

app.directive('depositList', ['depositService', function (depositService) { 
    return { 
     templateUrl: 'depositList.html', 
     restrict: 'E', 
     scope: { 
      status: '@status', 
      title: '@title' 
     }, 
     link: function (scope) { 
      scope.depositsInfo = depositService.getDeposits({ 
       status: scope.status 
      }); 
     } 
    }; 
}]); 

सेवा अब के लिए मामूली बात है:

app.factory('depositService', function(){ 
    return { 
    getDeposits: function(criteria){ 
     return 'you searched for : ' + criteria.status; 
    } 
    }; 
}); 

मैं मैं एक परीक्षण लिखने की कोशिश कर रहा हूं जो सुनिश्चित करता है कि depositService.getDeposits() सही स्थिति मान के साथ कहा जाता है। क्योंकि बार === 0. इस कोड को ब्राउज़र में ठीक चलाता

describe('Testing the directive', function() { 
    beforeEach(module('plunker')); 
    it('should query for pending deposits', inject(function ($rootScope, $compile, $httpBackend, depositService) { 

     spyOn(depositService, 'getDeposits').and.callFake(function(criteria){ 
     return 'blah'; 
     }); 

     $httpBackend.when('GET', 'depositList.html') 
      .respond('<div></div>'); 

     var elementString = '<deposit-list status="pending" title="blah"></deposit-list>'; 
     var element = angular.element(elementString); 
     var scope = $rootScope.$new(); 
     $compile(element)(scope); 
     scope.$digest(); 

     var times = depositService.getDeposits.calls.all().length; 
     expect(times).toBe(1); 
    })); 
}); 

परीक्षण विफल रहता है, लेकिन परीक्षा में link समारोह और सेवा कभी नहीं कहा जा लगते हैं। कोई विचार?

plunker: http://plnkr.co/edit/69jK8c

उत्तर

14

आप $httpBackend.flush() है, जो नकली $httpBackend बताता है एक टेम्पलेट वापस जाने के लिए लापता हैं। टेम्पलेट कभी लोड नहीं हो रहा था, इसलिए निर्देश लिंक फ़ंक्शन के पास लिंक करने के लिए कुछ भी नहीं था।

फिक्स्ड plunker: http://plnkr.co/edit/ylgRrz?p=preview

कोड:

describe('Testing the directive', function() { 
    beforeEach(module('plunker')); 
    it('should query for pending deposits', inject(function ($rootScope, $compile, $httpBackend, depositService) { 

     spyOn(depositService, 'getDeposits').and.callFake(function(criteria){ 
     return 'blah'; 
     }); 

     $httpBackend.when('GET', 'depositList.html') 
      .respond('<div></div>'); 

     var elementString = '<deposit-list status="pending" title="blah"></deposit-list>'; 
     var element = angular.element(elementString); 
     var scope = $rootScope.$new(); 
     $compile(element)(scope); 
     scope.$digest(); 

     $httpBackend.flush(); 

     var times = depositService.getDeposits.calls.all().length; 
     expect(times).toBe(1); 
    })); 
}); 
+0

मैं मुझे बहुत ही बड़ी जानकारी के इस टुकड़े को खोजने के लिए ले लिया। धन्यवाद! :) – Tudmotu

+0

इससे मुझे बहुत मदद मिली, धन्यवाद! –

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