Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.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

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 如何在Angular指令的单元测试中模拟jQuery方法_Angularjs_Unit Testing - Fatal编程技术网

Angularjs 如何在Angular指令的单元测试中模拟jQuery方法

Angularjs 如何在Angular指令的单元测试中模拟jQuery方法,angularjs,unit-testing,Angularjs,Unit Testing,我尝试测试一个简单的指令,该指令允许用户在单击给定元素后选择整个文本。但是我坚持了下来,因为我不知道如何测试元素的调用。select() 下面是一把小提琴: 指示 试验 在您的示例“错误:预期为间谍,但未定义…” 您应该使用spy on element.select方法: spyOn(元素“select”) 阅读更多信息:谢谢!我以前试过(应该提到),但似乎不起作用预期spy select已被调用。请看这里:我在处理程序中添加了html和console.log(“测试”)。在控制台中,我看到它工

我尝试测试一个简单的指令,该指令允许用户在单击给定元素后选择整个文本。但是我坚持了下来,因为我不知道如何测试
元素的调用。select()

下面是一把小提琴:

指示 试验 在您的示例“错误:预期为间谍,但未定义…”

您应该使用spy on element.select方法:

spyOn(元素“select”)


阅读更多信息:

谢谢!我以前试过(应该提到),但似乎不起作用<代码>预期spy select已被调用。
请看这里:我在处理程序中添加了html和console.log(“测试”)。在控制台中,我看到它工作。。。测试中出现问题。。。调查如果您添加
ele.select()就在
expect(ele.select).tohavebeencall()之前,测试(显然)通过了。是的,您是否尝试过使用karma和PhantomJS运行测试?可能是JSFIDDLE的问题,我已经让它工作了!查看我的更新。在此处找到提示:
/**
 * @ngdoc directive
 * @name Common.directive:clickSelect
 * @restrict A
 * @element ANY
 */
angular.module('Common').directive('clickSelect', function () {
    return {
        restrict: 'A',
        link: function (scope, element) {
            element.bind('click', function () {
                element.select();
            });
        }
    };
});
/**
 * @module test.Common
 * @name clickSelect
 */
describe('Directive: Common.clickSelect', function () {
    var ele, scope;

    beforeEach(module('Common'));
    beforeEach(inject(function ($compile, $rootScope) {
        scope = $rootScope.$new();
        ele = angular.element('<div><input click-select type="text" class="link" readonly /></div>');
        $compile(ele)(scope);
        scope.$apply();
    }));

    it('should render html', function () {
        expect(ele.length).toBe(1);
    });

    it('should select the text after click', function () {
        ele.trigger('click');
        // does not work.
        expect(ele.select).toHaveBeenCalled();
    });
});
it('should select the text after click', function () {
    spyOn($.fn, 'select').and.callThrough();
    ele.trigger('click');
    scope.$apply();
    expect($.fn.select).toHaveBeenCalled();
});