Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine array.length expect

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?

like image 803
MegaWubs Avatar asked Apr 28 '14 17:04

MegaWubs


2 Answers

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
});
});
like image 137
Matas Vaitkevicius Avatar answered Nov 18 '22 15:11

Matas Vaitkevicius


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();
    });
});
like image 3
Amy Avatar answered Nov 18 '22 15:11

Amy