我正在使用mocha和sinon进行测试,以从HTTP请求的promise范围内获取回调值,但由于promise的异步性质,它不起作用。这是因为当sinon.spy检查回调时,它已经消失了并变为空或未定义。以下是测试代码:
it('should issue GET /messages ', function() {
server.respondWith('GET', `${apiUrl}/messages?counter=0`, JSON.stringify([]));
let callback = sinon.spy();
Babble.getMessages(0, callback);
server.respond();
sinon.assert.calledWith(callback, []);
});
并承诺:
function requestPoll(props) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open(props.method, props.action);
xhr.timeout = 500; // time in milliseconds
if (props.method === 'post' ) {
xhr.setRequestHeader('Content-Type', 'application/json');
}
xhr.addEventListener('load', function(e) {
resolve(e.target.responseText);
});
xhr.send(JSON.stringify(props.data));
});
}
我正在尝试从sinon.spy回调中获取的调用
getMessages: function(counter, callback){
requestPoll({
method: "GET",
action: "http://localhost:9090/messages?counter="+counter
}).then(function(result){
callback(result);
});
}
sinon.spy 表示它没有任何参数(由于异步功能)。我试图寻找一种方法来获取作用域外的结果并将其放在回调中,但我发现这是不可能的。我也尝试了解决和承诺返回,但没有成功。
如何让这个单元测试通过?
编辑:
这是我的尝试:
getMessages: function(counter, callback){
var res;
res = httpRequestAsync("GET",'',"http://localhost:9097/messages?counter=",counter);
console.log(res);
if(res!="")
callback( JSON.parse(res) );
}
我将请求放在一个单独的函数中:
我将请求放在一个单独的函数中:
function httpRequestAsync(method,data,theUrl,counter)
{
return requestPoll({
method: method,
action: theUrl+counter,
data: data
}).then(JSON.parse);
}
我该如何访问那个承诺的值?
then
,并在表达式前面加上return
。请展示一下你的尝试。 - Bergireturn requestPoll({…});
以外的所有行,并删除callback
参数。如果需要解析结果,请在其后添加.then(JSON.parse)
。然后更改所有调用,使用 promise 替代传递回调函数。 - Bergi