我有角度js的指令。这增加了一个' src'属性为image元素,如下所示。
myApp.directive('imageonload', ['$q',function($q) {
var dPromise;
function loadImg(scope,target,url)
{
var deferred = $q.defer();
dPromise = deferred.promise;
var img = new Image();
// onload handler (on the image, not the element)
img.onload = function() {
img.onload = null;
//target.src = url;
deferred.resolve(url);
};
img.onerror = function() {
img.onerror = null;
//target.src = url;
deferred.resolve(url);
};
// set the source for the image to initiate the load
img.src = url;
return dPromise;
}
return {
restrict: 'A',
link: function(scope, element, attrs) {
loadImg(scope,element,attrs.url).then(function(data){
element[0].setAttribute("src",attrs.url);
scope.updateLoadCount();
});
}
};
}]);
现在我想测试是否使用jasmine测试用例将属性src添加到元素中。
我写的规范就像
it("should set the url after loading image",function(){
elm = angular.element("<img id='tnl_pic_2' class='ng-scope' url='http://tnl.sf-stg.com/assetrenderer/v2/thumbnail/SNAPFISH/iDY2vVuWYSbXroWYw54TNw/a/xwjOJEip4rCDO5d_fxw16A/d/qhac9fJtVzxe-Z2ExgA-lg/time/beLU_cFm0vcBwk85rnr7bg2015?height=160' imageonload=''>");
elm = compile(elm)(scope);
scope.$digest();
scope.img = new Image();
expect(elm.attr('src')).not.toBe('');
});
但是我的规范总是失败并且指令没有覆盖,src属性永远不会添加到元素
由于我是茉莉花和棱角分明的初学者,请你帮我解决一下如何测试这个指令的功能。 谢谢你!