Reputation: 40134
I am running Sinon's fake XMLHttpRequest function and I am not able to match the results in my test when checking with calledWith
.
Test - partially taken from docs:
// 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' ));
If I dump callback.getCall(0).args[0]
I get this object:
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' } }
My internal code is the one sending the { key: '12' }
request. I am not sure what calledWith is really referring to so I tried testing for both response and request but both fail.
Any idea why it's failing - how can I tell what it is actually being "called with"?
Upvotes: 0
Views: 224
Reputation: 40134
Ok solved. My callback method was returning the entire xhr
object and the examples assume you are only returning xhr.response
.
Upvotes: 1