如何检查是否在Jasmine中使用特定选择器调用了jQuery,$? [英] How to check if jQuery, $ is called with a specific selector in Jasmine?

查看:55
本文介绍了如何检查是否在Jasmine中使用特定选择器调用了jQuery,$?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

$('#test1').val('string1');

我可以测试是否已使用'string1'调用了 val .

I'm able to test if val has been called with 'string1'.

spyOn($.fn, 'val');

expect($.fn.val).toHaveBeenCalledWith('string1');

但是我想测试$是否已通过特定选择器调用.我希望能够做到这一点.

But I want to test if $ has been called with a specific selector. I want to be able to do this.

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

推荐答案

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

Looking at the jQuery script (version 3.5.1), I found that the function that handles the selector is $.fn.init

 jQuery.fn.init = function( selector, context, root ) {...}

在#3133行.

因此您的茉莉花期望为:

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


您可以忽略一些参数.参考

这篇关于如何检查是否在Jasmine中使用特定选择器调用了jQuery,$?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆