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 $scope
f2
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
tarihinde preventDefault çağırmalıdır: Yani böyle bir şey olurdu? –
@EvanBechtol, umarım şimdi daha net – rossoneri