vendredi 2 janvier 2015

Simple Jasmine Async Test causes timeout

I am struggling to get a simple test to run on an async function in a module. I am trying to get my head around testing and so have built a working module and manually tested it in repl.it


http://repl.it/7Qf


My module looks like this:



var weather = (function() {
var location;
var url = 'http://ift.tt/1BrzXCH';

function getData(loc, callback) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url+loc, true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
} else {
callback(JSON.parse(xhr.statusText));
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);
}

return {
getTemp: function(location, callback) {
getData(location, function(data) {
var temp = (data.main.temp - 273.15).toFixed(2);
callback(temp);
});
}
}
}());


My test suite looks like this:



describe("Weather Module", function() {
it('get temperature', function(done) {
weather.getTemp('coventry,uk', function(data){
console.log(data);
expect(data).toBe(2.66);
done();
});
});
});


I get a timeout error when I run this test. Can anyone see where I am going wrong?


Aucun commentaire:

Enregistrer un commentaire