Javascript Mocha Sinon测试ajax请求(在节点中)

Javascript Mocha Sinon测试ajax请求(在节点中),javascript,node.js,mocha.js,sinon,chai,Javascript,Node.js,Mocha.js,Sinon,Chai,如何使用mocha和sinon测试Ajax请求 下面是名为testApp的类中的初始化函数: testApp.prototype.initialize=function() { $.get(....); } 在测试中,如果我说 sinon.stub($,"get", function(d,e) {}); var test = new testApp(); 此行抛出错误$未定义 我用grunt来运行mochatests 我尝试使用$和jQuery。但我总是得到未定义的变量 有人能帮忙吗?

如何使用mocha和sinon测试Ajax请求

下面是名为testApp的类中的初始化函数:

 testApp.prototype.initialize=function() {
  $.get(....);
}
在测试中,如果我说

sinon.stub($,"get", function(d,e) {});
var test = new testApp();
此行抛出错误$未定义

我用grunt来运行mochatests

我尝试使用$和jQuery。但我总是得到未定义的变量


有人能帮忙吗?

这是在节点环境中测试的单元测试解决方案。因此,我使用
jsdom
jQuery
提供模拟窗口。否则,
$.ajax
$.get
方法将是
未定义的

index.js

const$=require(“./jquery”);
函数TestApp(){}
TestApp.prototype.initialize=函数(){
$.get(”https://github.com/mrdulin");
};
module.exports=TestApp;
jquery.js

var jsdom=require(“jsdom”);
const{JSDOM}=JSDOM;
const{window}=newjsdom();
const{document}=newJSDOM(“”).window;
global.document=文件;
var$=require(“jquery”)(窗口);
module.exports=$;
index.spec.js

const$=require(“./jquery”);
const sinon=要求(“sinon”);
const TestApp=require(“.”);
描述(“TestApp”,()=>{
它(“应该初始化”,()=>{
const getStub=sinon.stub($,“get”);
const test=new TestApp();
test.initialize();
sinon.assert.calledWith(getStub,“https://github.com/mrdulin");
});
});
100%覆盖率的单元测试结果:

TestApp
✓ 应该初始化
1次通过(38ms)
---------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
---------------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 100 | 100 ||
index.js | 100 | 100 | 100 | 100 ||
index.spec.js | 100 | 100 | 100 | 100 ||
jquery.js | 100 | 100 | 100 | 100 ||
---------------|----------|----------|----------|----------|-------------------|

源代码:

这是在节点环境中测试的单元测试解决方案。因此,我使用
jsdom
jQuery
提供模拟窗口。否则,
$.ajax
$.get
方法将是
未定义的

index.js

const$=require(“./jquery”);
函数TestApp(){}
TestApp.prototype.initialize=函数(){
$.get(”https://github.com/mrdulin");
};
module.exports=TestApp;
jquery.js

var jsdom=require(“jsdom”);
const{JSDOM}=JSDOM;
const{window}=newjsdom();
const{document}=newJSDOM(“”).window;
global.document=文件;
var$=require(“jquery”)(窗口);
module.exports=$;
index.spec.js

const$=require(“./jquery”);
const sinon=要求(“sinon”);
const TestApp=require(“.”);
描述(“TestApp”,()=>{
它(“应该初始化”,()=>{
const getStub=sinon.stub($,“get”);
const test=new TestApp();
test.initialize();
sinon.assert.calledWith(getStub,“https://github.com/mrdulin");
});
});
100%覆盖率的单元测试结果:

TestApp
✓ 应该初始化
1次通过(38ms)
---------------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
---------------|----------|----------|----------|----------|-------------------|
所有文件| 100 | 100 | 100 | 100 ||
index.js | 100 | 100 | 100 | 100 ||
index.spec.js | 100 | 100 | 100 | 100 ||
jquery.js | 100 | 100 | 100 | 100 ||
---------------|----------|----------|----------|----------|-------------------|

源代码:

您得到的错误肯定意味着jQuery没有加载!一旦jQuery被加载,您就可以这样做了。。。但是我肯定会使用sinon.spy($,“get”)并检查它是否被调用,有没有所需的参数,然后恢复spy。如果您想在独立模式下进行测试,那么我将使用sinon.FakeXML/Server!您使用什么版本的SinonJS?您得到的错误肯定意味着jQuery没有加载!一旦jQuery被加载,您就可以这样做了。。。但是我肯定会使用sinon.spy($,“get”)并检查它是否被调用,有没有所需的参数,然后恢复spy。如果您想在独立模式下进行测试,那么我将使用sinon.FakeXML/Server!您使用什么版本的SinonJS?