2016-08-31 16 views
5

işlevi tarafından yürütülen kontrolör kapsamında işlevini test etmek için:nasıl denetleyicisi olay

angular.module('myApp').controller('MyController', function(){ 

    $scope.f = function($event){ 
     $event.preventDefault(); 
     //logic 
     return data; 
    } 
}) 

describe('MyController', function(){ 
    'use strict'; 
    var MyController, 
     $scope; 

    beforeEach(module('myApp')); 

    beforeEach($inject(function($rootScope, $controller){ 
     $scope = $rootScope.$new(); 
     MyController = $controller('MyController', { 
      $scope: $scope 
     }) 
    })); 
}) 
it('should...', function(){ 
    //fire event and expect data 
}) 

$scope.f fonksiyon yönergesi kullanılır, bu ng-click="f($event)"

tarafından çalıştırılabilir ünitesinde yangın olay için doğru yol nedir Ölçek?

+1

tarihinde preventDefault çağırmalıdır: Yani böyle bir şey olurdu? –

+0

@EvanBechtol, umarım şimdi daha net – rossoneri

cevap

4

Kısa Cevap

Sen olayı gerekmez. Test etmek istediğiniz işleve sahip olan kapsama erişebilirsiniz. Bu sadece işlevi yürütmek, sonra iddia etmek anlamına gelir. Böyle bir şey olacaktır:

it('should call preventDefault on the given event', function(){ 
    var testEvent = $.Event('someEvent'); 
    $scope.f(testEvent); 
    expect(testEvent.isDefaultPrevented()).toBe(true); 
}); 

aşağıdaki bakınız:

Tam Spec Ayrıca

- senin it blok b gerektiğini describe bloğunuzun içinde, böylece $scope alanına erişim sağlayın. Böyle daha görünmelidir:

describe('MyController', function(){ 
    'use strict'; 
    var MyController, 
     $scope; 

    beforeEach(module('myApp')); 

    beforeEach($inject(function($rootScope, $controller){ 
    $scope = $rootScope.$new(); 
    MyController = $controller('MyController', { 
     $scope: $scope 
    }) 
    })); 

    it('should call preventDefault on the given event', function(){ 
    var testEvent = $.Event('someEvent'); 
    $scope.f(testEvent); 
    expect(testEvent.isDefaultPrevented()).toBe(true); 
    }); 
}) 

Bir Not

testlerinizi yapılandırmaya describe blokları kullanmak korkmayın Yapı sağlar. o zaman muhtemelen Spec daha böyle yukarı dosyası bölme isteyeyim, sen $scopef2 denilen başka bir işlevi vardı düşünün: Bu bir denemedir başarısız olduğunda, gördüğünüz hata iletisi oluşturulur olduğunu yararı vardır

describe('MyController', function(){ 
    'use strict'; 
    var MyController, 
     $scope; 

    beforeEach(module('myApp')); 

    beforeEach($inject(function($rootScope, $controller){ 
    $scope = $rootScope.$new(); 
    MyController = $controller('MyController', { 
     $scope: $scope 
    }) 
    })); 

    describe('$scope', function() { 
    describe('.f()', function() { 
     // tests related to only the .f() function 
    }); 

    describe('.f2()', function() { 
     // tests related to only the .f2() function 
    }); 
    }); 
}) 

describe bloklarının hiyerarşisine dayanmaktadır.

MyController $ kapsamı .F() biraz daha bağlamsal bilgiler verebilir misiniz verilen olay