Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.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
Javascript 如何检查Jasmine中是否使用特定选择器调用了jQuery$?_Javascript_Jquery_Unit Testing_Jasmine - Fatal编程技术网

Javascript 如何检查Jasmine中是否使用特定选择器调用了jQuery$?

Javascript 如何检查Jasmine中是否使用特定选择器调用了jQuery$?,javascript,jquery,unit-testing,jasmine,Javascript,Jquery,Unit Testing,Jasmine,我的代码: $('#test1').val('string1'); 我能够测试是否使用'string1'调用了val spyOn($.fn, 'val'); expect($.fn.val).toHaveBeenCalledWith('string1'); 但我想测试是否使用特定选择器调用了$。我希望能够做到这一点 expect($.fn).toHaveBeenCalledWith('#test1'); 查看jQuery脚本(版本3.5.1),我发现处理选择器的函数是$.fn.init

我的代码:

$('#test1').val('string1');
我能够测试是否使用'string1'调用了val

spyOn($.fn, 'val');

expect($.fn.val).toHaveBeenCalledWith('string1');
但我想测试是否使用特定选择器调用了$。我希望能够做到这一点

expect($.fn).toHaveBeenCalledWith('#test1');

查看jQuery脚本(版本3.5.1),我发现处理选择器的函数是
$.fn.init

 jQuery.fn.init = function( selector, context, root ) {...}
那是第3133行

因此,您的Jasmine
expect
将是:

expect($.fn.init).toHaveBeenCalledWith('#test1', undefined, jasmine.anything());


你可以忽略一些论点

非常感谢。这起作用了。它抛出了一个错误。但是我只需要添加undefined和jasmine.anywhere()作为附加的2个参数。错误:应使用:['#test1']调用spy init,但实际调用为:['#test1',未定义,({0:HTMLNode,context:HTMLNode,length:1})],我已相应编辑;)你能接受答案(绿色复选标记)吗?