我刚刚开始进行 jasmine 单元测试,并且在测试我的异步调用时遇到了一些麻烦。
我有一个要测试的 ajax 调用,我在控制台中尝试过它,所以我知道它按我想要的方式工作。我想我正在测试我在控制台中做的同样的事情,但可能是错误的。
这是控制台:
> 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
设置本地数据只是在向外部服务器发送 http 请求或从本地文件加载数据之间发生变化。
茉莉花测试代码在这里:
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();
            });
        });
    });
});
第一次测试通过。但随后第二个给出了错误timeout: timed out after 5000 msec waiting for something to happen。我尝试按照教程进行操作,但显然不是很好。
谢谢您,非常感谢您的帮助!