jeudi 29 octobre 2015

Jasmine testing get request after promise. Error: No pending request to flush?

I need your help. I'm getting an error: Error: No pending request to flush !

Here is my factory:

angular.module('myapp')
.factory('WebDB', function ($http, LocalDB) {
    function lastChangeParameter(resource) {
        return new Promise(function (resolve, reject) {
            LocalDB.getLastChange(resource).then(function (time) {
                resolve("last_change=" + time);
            });
        }); 
    }
    return {
        getBuildings: function () {
            return lastChangeParameter("buildings").then(function (lastChange) {
                return get(sourceDB + "buildings" + "?" + lastChange);
        });
    }};
});

And test:

describe('Testing webdb module...', function () {
var WebDB;
var httpBackend;
var mockedLocalDB;
var $rootScope;

beforeEach(function () {
    module("myapp");

    mockedLocalDB = {
        getLastChange: function (resource) {
            return new Promise(function (resolve, reject) {
                resolve(0);
            });
        }
    };
    module(function ($provide) {
        $provide.value('LocalDB', mockedLocalDB);
    });

    inject(function ($injector) {
        WebDB = $injector.get("WebDB");
        httpBackend = $injector.get("$httpBackend");
        $rootScope = $injector.get("$rootScope");
    });
});



afterEach(function () {
    httpBackend.verifyNoOutstandingExpectation();
    httpBackend.verifyNoOutstandingRequest();
});

describe("WebDB", function () {
    describe("getBuildings", function () {
        it("should download updated buildings", function () {
            var respondData = {};
            httpBackend.expectGET('http://ift.tt/20evRft').respond(200, respondData);


            WebDB.getBuildings().then(function (buildings) {
                result = buildings;
            });
            $rootScope.$digest();
            httpBackend.flush();
            expect(result).toEqual(respondData);
        });
    });

I think this happens because before get request I'm calling some db function, which is promise. Without that call it works perfectly, but I really need to ask local DB for the lastChange value before get request. What's wrong with my test or factory? Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire