异步请求的Jasmine 2.0超时错误

时间:2016-02-07 00:50:27

标签: javascript angularjs jasmine

最近我开始用Jasmine来测试我的JavaScript应用程序。我尝试为异步请求实现我的第一个测试,但是我收到超时错误,我不知道为什么。

我的后端服务:

formularGenerator.factory("backendConnector", ["$http", function ($http) {
    var BC = {};

    BC.getFormularSpecification = function (callback) {
        $http({
            method: 'GET',
            url: 'http://localhost:8000/response.json'
        }).then(function (response, status) {
            callback(response.data);
        },function (error){
            callback(error);
        });
    }

    return BC;
}]);

我对异步请求的测试:

describe("backendConnector", function() {
    var backendConnector, httpBackend, fsResponse;

    beforeEach(module('formularGenerator'));

    beforeEach(inject(function (_backendConnector_, $httpBackend) {
        backendConnector = _backendConnector_;
        httpBackend = $httpBackend;
    }));

    beforeEach(function(done) {
        backendConnector.getFormularSpecification(
            function(formularSpecification) {
                fsResponse = formularSpecification;
                done();
            }
        )
    });

    it("should not return an empty JSON", function() {
        expect(fsResponse).not.toEqual({});
    });
});

我的错误:

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

为什么会出现此错误?请求可能是异步的,但在我的情况下,它绝对不会超过jasmine的超时间隔。 (我甚至试图延长超时间隔......)

如何让我的测试工作?

0 个答案:

没有答案