简体   繁体   中英

Jasmine async call trouble

I am just getting started with jasmine unit testing and am running into some trouble with testing my async calls.

I have a ajax call that I am trying to test, and I tried it in the console so I know it works how I want it to. I think I am testing the same thing that I did in the console but could be wrong.

Here is the console:

> mg = new MandellMVC()
MandellMVC {getSessionId: function, setSessionId: function, isValidGetFunction: function, getURLPrefix: function, setURLPrefix: function…}
> mg.setUseLocalData(true);
true
> var log = new Log(73936780)
undefined
> log.setLogType('Proc')
true
> log.fetch(mg, function(){console.log('done');})
true
done

set local data just changes between sending an http request to an external server, or loading data from a local file.

The jasmine test code is here:

describe("Log Model", function() {

    var mg = new MandellMVC();
    mg.setUseLocalData(true);

    var log;

    beforeEach(function() {
        log = new Log(73936780);
    });

    describe("function fetch", function() {
         it("returns false if log type is invalid", function() {
            expect(log.fetch(mg, function(){})).toBeFalsy();
        });

        // Not sure why this needs to be here too?
        log = new Log(73936780);
        log.setLogType('Proc');

        it("should make a real ajax request", function() {
            var callback = jasmine.createSpy();
            log.fetch(mg, callback);
            waitsFor(function() {
                return callback.callCount > 0;
            });
            runs(function() {
                expect(callback).toHaveBeenCalled();
            });
        });
    });
});

The first test passes. But then the second one gives the error timeout: timed out after 5000 msec waiting for something to happen . I tried to follow a tutorial but apparently not very well.

Thank you, any help is much appreciated!

i think you are mixing up declaration of log = new Log(73936780)

the correct code should be

describe("Log Model", function() {
    var mg;
    var log;

    beforeEach(function() {
        mg = new MandellMVC();
        mg.setUseLocalData(true);

        log = new Log(73936780);
        log.setLogType('Proc');
    });

    afterEach(function() {
        delete mg;
        delete log;
    });

    describe("function fetch", function() {
        it("returns false if log type is invalid", function() {
            expect(log.fetch(mg, function(){})).toBeFalsy();
        });

        it("should make a real ajax request", function() {
            var callback = jasmine.createSpy();
            log.fetch(mg, callback);
            waitsFor(function() {
                return callback.callCount > 0;
            });
            runs(function() {
                expect(callback).toHaveBeenCalled();
            });
        });
    });
});

Additionally you should also check in firebug if the request is sent and response is received correctly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM