2
describe("create a simple directive", function() {
var simpleModule = angular.module("directivesSample", []);
simpleModule.controller('Ctrl2', function ($scope) {
$scope.format = 'M/d/yy h:mm:ss a';
});
simpleModule.directive("myCurrentTime", function ($timeout, dateFilter) {
return function (scope, element, attr) {
var format;
var timeoutId;
function updateTime() {
element.text(dateFilter(new Date(), format));
}
scope.$watch(attr.myCurrentTime, function (value) {
format = value;
updateTime();
});
function updateLater() {
timeoutId = $timeout(function() {
updateTime();
updateLater();
}, 1000);
}
element.bind('$destroy', function() {
$timeout.cancel(timeoutId);
});
updateLater();
}
});
beforeEach(module('directivesSample'));
var element = angular.element(
'<div ng-controller="Ctrl2">Date format:<input ng-model="format"> ' +
'<hr/>Current time is: ' +
'<span class="timeout" my-current-time="format" id="timeout-render"></span>' +
'</div>');
var directiveScope;
var scope;
var linkedElement;
var linkFunction;
beforeEach(inject(function ($rootScope, $compile) {
scope = $rootScope.$new();
linkFunction = $compile(element);
linkedElement = linkFunction(scope);
scope.$apply();
}));
it("should define element time out", function() {
var angularElement = element.find('span'); // <-- element is not returned if set to var angularElement = element.find('.timeout'); or var angularElement = element.find('#timeout-render');
console.log(angularElement.text());
expect(angularElement.text()).not.toBe('');
})
});
通過上述測試,爲什麼我無法通過JQuery選擇器搜索元素?我知道的find()method.But文檔中的limitations,我已經簽出了angularUI項目考察發現的使用()函數中here如何使用angular.element查找函數通過JQuery選擇器進行搜索?
var tt = angular.element(elm.find("li > span")[0]);
,並發現該人是使用find來通過jQuery elector搜索元素不僅可以標記名稱,而且我也不能這樣做。我錯過了什麼嗎?
我已經在testacular.conf.js中包含jQuery(我想這應該包含它),但仍然無法工作! – Adelin 2013-02-26 14:57:56
但是,你的答案似乎是合理的! – Adelin 2013-02-26 14:58:34
您必須在測試前明確包含它以避免競爭條件。 – 2013-04-04 22:55:31