I’m using a function to fetch data from webapi. Basicly using $.ajax.
I’m now testing it with waits() like this:
describe('xxxxxxxxxxxxxxxxxxxxx', function () {
var r;
it('fetchFilter', function () {
runs(function () {
model.fetch(opts)
.done(function(data) {
r = data;
});
});
waits(2000);
runs(function () {
expect(r[0].gender).toBeDefined();
});
});
});
The problem is:
- It’s not guaranteed that
waits(2000)will do the job well. Due to various reasons(network connections, algorithm efficiency of the api it self, etc.), I may have towaits(5000)or more, or for some modelswaits(500)is enough. And the most annoying thing is that it’s all out of control. - A lot of
waits()makes the test-specs-runs waste a lot of time waiting. The time of running the whole suite is too long to accept.
Is there some best practice of doing there kind of things?
PS: I know that unit test should not be applied to some function that relies on webapi or database. But I’m working with a single-page-js-heavy-webapp. The data fetching process is as important as how I will consume them with js models.
waitsFor()will wait for a specified latch callback to returntrue(it will try many time every few ms). It will also raise an exception if the specified timeout (5000ms in this case) is exceeded.Check the Jasmine docs for more info on
waitsFor()andruns()