2014-06-24 6 views
8

onun link fonksiyonunda bir hizmeti çağıran bir eleman yönergesini kuruyorum yasemin testinde aramadı:angularjs yönergesi bağlantı fonksiyonu

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

hizmet şimdilik Önemsiz:

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

ben depositService.getDeposits()'un doğru durum değeriyle çağrılmasını sağlayan bir test yazmaya çalışıyorum. Zaman === 0. Bu kod tarayıcıda iyi çalışır çünkü

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

Test başarısız, ancak test link fonksiyon ve hizmet asla çağrılacak görünüyor. Düşüncesi olan var mı?

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

cevap

14

Bir şablon dönmek için sahte $httpBackend söyler $httpBackend.flush() kaybolmuştu. Şablon asla yüklenmemiştir, bu yüzden yönerge bağlantısı işlevinin bağlantı kuramayacağı hiçbir şey yoktu.

Sabit plunker: http://plnkr.co/edit/ylgRrz?p=preview

kodu:

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

Ben bu bilgiyi üst bulmam bu yol çok uzun sürdü. Teşekkürler! :) – Tudmotu

+0

Bu bana çok yardımcı oldu, teşekkürler! –