使用Jasmine单元测试测试Jquery选择器

编程入门 行业动态 更新时间:2024-10-28 04:22:06
本文介绍了使用Jasmine单元测试测试Jquery选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 $('.froala > div > div > div > p').each(function (index, element) { if ($(element).text() !== '') { wordCount += $(element).text().split(' ').length; } });

我有这个代码可以获得所有<在froala编辑器中标记并计算它们。我需要编写一个Jasmine单元测试来覆盖这个,我不知道如何做到这一点。也许我可以使用spyOn并返回一个< p>标签...

I have this code that gets all the < p > tags in the froala editor and counts them. I need to write a Jasmine unit test to cover this and I don't have a clue how to do that. Maybe I could use a spyOn and return an array of < p > tags...

spyOn($('.froala > div > div > div > p'), 'each').and.returnValue([all, my, tags, here]);

还有其他想法吗?

推荐答案

你不应该真正编写测试来测试jQuery,而jasmine并不是要测试dom。您应该只为自己的代码编写测试,并具有确定性的输入/输出。

You shouldn't really be writing tests to test jQuery, and jasmine is not meant to test the dom. You should only write tests for your own code, and have deterministic input/output.

var myFunction = function(index, element) { if ($(element).text() !== '') { wordCount += $(element).text().split(' ').length; } } var jquerySelector = ".froala > div > div > div > p"; $(jquerySelector).each(myFunction });

然后编写导入上面文件并具有预期froala html的jasmine测试

Then write jasmine tests that import above file and have the expected froala html

var wordCount = 0; var testhtml = '<div class="froala"><div><div><div><p>one</p><p>two</p></div></div></div></div>'; $(testhtml).find(jquerySelector).each(myFunction) expect( wordCount ).toEqual(2)

更多推荐

使用Jasmine单元测试测试Jquery选择器

本文发布于:2023-06-03 12:14:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/473701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   测试   选择器   Jasmine   Jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!