简体   繁体   English

如何在javascript单元测试中模拟数据?

[英]How to mock data in javascript unit testing ?

I m using Jasmine library for unit testing my javascript code. 我使用Jasmine库来测试我的javascript代码。 I have tried to find a way to mock data, but I didn't found a good solution. 我试图找到一种模拟数据的方法,但我没有找到一个好的解决方案。 Information on how to mock data and libraries which mock data automatically with an example would help me a lot. 有关如何使用示例自动模拟数据的模拟数据和库的信息对我有很大帮助。 Thanks in advance. 提前致谢。

Sinon.js is a generic, framework-agnostic library that enables you to mock entirely data in the browser (by faking XMLHttpRequest Object). Sinon.js是一个通用的,与框架无关的库,它允许您在浏览器中完全模拟数据(通过伪造XMLHttpRequest对象)。 You can use it with jasmine eg this way . 你可以用这种方式与茉莉花一起使用。 What this library does is extending jasmine assertions with native sinon features, such as called , threw , returned , etc. See them all here . 这个库的作用是扩展具有原生sinon特征的jasmine断言,例如被calledthrewreturned等等。 在这里查看所有这些

In Sinon, you should take a look at fakeServer . 在Sinon,你应该看看fakeServer

Additionally, here you've got a working example of sinon's fakeServer along with jasmine runner written by the author of Sinon.js: https://gist.github.com/cjohansen/739589 :) 另外,在这里你有一个sinon的fakeServer以及由Sinon.js的作者编写的jasmine runner的工作示例: https ://gist.github.com/cjohansen/739589 :)

And an in-depth introdution to AJAX/sinon: http://tutorials.jumpstartlab.com/projects/javascript/testing/2-ajax-and-sinon.html 并对AJAX / sinon进行了深入介绍: http ://tutorials.jumpstartlab.com/projects/javascript/testing/2-ajax-and-sinon.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM