我正在运行Sinon的假XMLHttpRequest函数,在使用calledWith
进行检查时,我无法在测试中匹配结果。
测试 - 部分取自文档:
// New spy callback and make our call
var callback = sinon.spy();
myLib.getCommentsFor("/some/article", callback);
// Add a response to our request that we stored in `beforeEach`
this.requests[0].respond(200, { "Content-Type": "application/json" },
'[{ "id": 12, "comment": "Hey there" }]');
// Check - both of these fail
assert(callback.calledWith([{ id: 12, comment: "Hey there" }]));
assert(callback.calledWith({ key: '12' ));
如果我转储callback.getCall(0).args[0]
,我会得到这个对象:
FakeXMLHttpRequest {
readyState: 4,
requestHeaders: { 'Content-type': 'application/x-www-form-urlencoded;charset=utf-8' },
requestBody: { key: '12' },
status: 200,
statusText: 'OK',
upload:
UploadProgress {
eventListeners: { progress: [], load: [], abort: [], error: [] } },
responseType: '',
response: '[{ "id": 12, "comment": "Hey there" }]',
eventListeners:
{ loadend: [ [Function] ],
abort: [ [Function] ],
load: [ [Function] ],
loadstart: [ [Function] ] },
method: 'POST',
url: 'http://localhost:3000',
async: true,
username: undefined,
password: undefined,
responseText: '[{ "id": 12, "comment": "Hey there" }]',
responseXML: null,
sendFlag: true,
onreadystatechange: [Function],
errorFlag: false,
responseHeaders: { 'Content-Type': 'application/json' } }
我的内部代码是发送{ key: '12' }
请求的代码。我不确定callWith究竟是指什么,所以我尝试测试响应和请求,但都失败了。
任何想法为什么会失败 - 我怎么能说出它实际上是什么“被称为”?
答案 0 :(得分:1)
好的解决了。我的回调方法是返回整个xhr
对象,示例假设您只返回xhr.response
。