How to mock JQuery with Jasmine?

╄→гoц情女王★ 提交于 2019-12-05 18:16:20

Well it looks like Jasmine does it's spy stuff by replacing the spied-on object with a wrapped version, and that seems to be messing up jQuery as a whole because (from the jQuery source code):

// The jQuery object is actually just the init constructor 'enhanced'

I'd suggest trying to spy on one of the functions that init uses, specifically "merge". If you look at the jQuery code, you'll see that any HTML=>DOM stuff ultimately gets returned through a merge call:

return jQuery.merge( this, selector );

(that's line 152 if you happen to be looking at the source of jQuery 1.5.1).

By spying on merge you should be able to test whatever you're testing, without inadvertently replacing the guts of jQuery.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!