Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何对具有绑定(';error';,function(){})的角度指令进行单元测试?_Angularjs_Unit Testing_Jasmine - Fatal编程技术网

Angularjs 如何对具有绑定(';error';,function(){})的角度指令进行单元测试?

Angularjs 如何对具有绑定(';error';,function(){})的角度指令进行单元测试?,angularjs,unit-testing,jasmine,Angularjs,Unit Testing,Jasmine,我有一个angular指令,如果文件不存在并且返回404,它将替换元素的image src。这是: myApp.directive('fallBackSrc', function() { var fallBackSrc = { link: function postLink(scope, element, attrs) { element.bind('error', function () { angular.ele

我有一个angular指令,如果文件不存在并且返回404,它将替换元素的image src。这是:

myApp.directive('fallBackSrc', function() {
    var fallBackSrc = {
        link: function postLink(scope, element, attrs) {
            element.bind('error', function () {
                angular.element(this).attr("src", '/images/image-not-available.png');
            });
        }
    }
    return fallBackSrc;
}]);

这正如预期的那样工作,但现在我正在尝试对该指令进行单元测试,我将如何触发jasmine中该函数绑定到的“错误”?

为了测试这一点,您需要使用spyOn元素.bind并同时使用jasmine 2.0 done函数

因此,在中的示例中,我添加了一个占位符图像作为替换图像,以阻止它导致错误

angular.module('components', [])
    .directive('fallBackSrc', function() {
        var fallBackSrc = {
            link: function postLink(scope, element, attrs) {
                element.bind('error', function () {
                    angular.element(this).attr("src", 'http://placehold.it/350x150');
                });
            }
        }
        return fallBackSrc;
    });
在测试中,我们可以
spyOn
element.bind
并在调用时返回
error

spyOn(element, 'bind').and.returnValue('error');
我们还需要使用done函数来运行需要测试异步操作的规范,方法是将其传递到要在
setTimeOut
中使用的测试中

describe('Directive: fallBackSrc', function() {
        describe('if the image does not exist it', function() {

        var element, scope;

        beforeEach(module('components'));

        beforeEach(inject(function($rootScope, $compile) {
            element = angular.element('<img fall-back-src src="images/no-image.jpg" />');
            spyOn(element, 'bind').and.returnValue('error');
            scope = $rootScope;
            $compile(element)(scope);
            scope.$digest();
        }));

        it("should contain the place holder img src", function(done) {          
            setTimeout(function(){  
                console.log(element.attr('src'));
                expect(element.attr('src')).toEqual('http://placehold.it/350x150'); 
                done();
            }, 2000);
        });
    });

});
description('指令:fallBackSrc',函数(){
description('如果图像不存在,'函数()){
var元素、范围;
在每个(模块(“组件”)之前;
beforeach(注入(函数($rootScope,$compile){
元素=角度。元素(“”);
spyOn(元素'bind')。和.returnValue('error');
scope=$rootScope;
$compile(元素)(范围);
范围。$digest();
}));
它(“应该包含占位符img src”),函数(done){
SETTIMEOUTE(函数(){
console.log(element.attr('src'));
expect(element.attr('src')).toEqual('http://placehold.it/350x150'); 
完成();
}, 2000);
});
});
});