'check POST data'
);
});
The mock we set on the mock navigator’s watch function lets us extract the
callback function we set as “onlogin.”
We can then actually call that function in order to test it.
Sinon’s
fakeXMLHttpRequest
server will catch any Ajax requests we make, and
put them into the
requests
array. We can then check on things like whether it
was a POST and what URL it was sent to.
The actual POST parameters are held in
.requestBody
, but they are URL-
encoded (using the &key=val syntax). jQuery’s
$.param
function does URL-
encoding, so we use that to do our comparison.
And the two tests fail as expected:
4. navigator.id.watch tests: onlogin does ajax post to login url (1, 0, 1)
1. Died on test #1
@file:///workspace/superlists/accounts/static/tests/tests.html:78:
onloginCallback is not a function
5. navigator.id.watch tests: onlogin sends assertion with csrf token (1, 0, 1)
1. Died on test #1
@file:///workspace/superlists/accounts/static/tests/tests.html:90:
onloginCallback is not a function
Another unit-test/code cycle. Here’s the failure messages I went through:
1. check ajax request
Expected: 1
…
3. check url
Expected: "login url"
…
7 assertions of 8 passed, 1 failed.
1. check POST data
Expected:
"assertion=browser-id+assertion&csrfmiddlewaretoken=csrf+token"
Result: null
…
1. check POST data
Expected:
"assertion=browser-id+assertion&csrfmiddlewaretoken=csrf+token"
Result: "assertion=browser-id+assertion"
…
JavaScript Unit Tests Involving External Components: Our First Mocks!
|
271