I'm just new to the jasmine js test framework and got some odd results today.
See the following code (search
is a function that preforms an api request and returns a promise):
it('should be able to search', function() {
search('string').done(function(result) {
expect(result.length).toBeGreaterThan(1); //true
console.log(result.lenght); // undefined
});
});
The thing is that, due to some errors that I have to fix, the result from the promise is undefined, but the test is marked as Success
. I find this misleading and if I din't investigate this deeply I would have believed that the test was a success while it clearly wasn't. Is this expected behaviour?
You have typo in console.log(result.lenght) please try this.
it('should be able to search', function() {
search('string').done(function(result) {
expect(result.length).toBeGreaterThan(1); //true
console.log(result.length); // undefined
});
});
For testing asynchronous functions, your tests need to be written slightly differently. From the latest Jasmine (2.0) documentation, asynchronous test are written as follows:
beforeEach(function(done) {
setTimeout(function() {
// do setup for spec here
// then call done() in beforeEach() to start asynchronous test
done();
}, 1);
});
it('should be able to search', function(done) {
search('string').done(function(result) {
expect(result.length).toBeGreaterThan(1); //true
// call done() in the spec when asynchronous test is complete
done();
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With