![](/img/trans.png)
[英]AngularJS – how to write unit-test in Jasmine for enter events directive
[英]best way to write Jasmine Test for DOM directive
我是Angular JS的Jasmine BDD測試的新手-誰能告訴我一個簡單的測試用例
我的指令
var app = angular.module('gls', []);
app.directive('changeBg', function() {
return {
restrict: 'A',
link: function($scope, element, attrs) {
element.bind('click', function(){
element.css('background','red');
});
}
};
});
標記
<body ng-app="gls">
<a href='#' change-bg>Test link 1</a>
</body>
通常,要測試指令,您需要創建一個范圍,使用該指令編譯一個簡單的模板,然后對其執行一些操作。 在這種情況下,操作就是單擊(就我所知,這需要jQuery)。
describe('Testing the changeBg directive', function() {
var element = null;
//you need to indicate your module in a test
beforeEach(module('plunker'));
beforeEach(inject(function($rootScope,$compile) {
var scope = $rootScope.$new();
var template = '<span change-bg></span>';
element = $compile(template)(scope);
element.click(); // Requires jQuery
}));
it('should be red after a click', function() {
expect(element.css('background')).toEqual('red');
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.