我的代码:
$('#test1').val('string1');
我可以测试 值 已通过 调用'字符串1' .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());
你可以忽略一些论点。 Reference
关于javascript - 如何检查是否使用 Jasmine 中的特定选择器调用了 jQuery、$?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65849906/