Angularjs cleaning service with promise

I am trying to unit test a service that uses a repository, which in turn returns a promise to the consumer. I'm having trouble validating a promise, or should I say I don't know how to validate a promise.
Any help would be appreciated!

+3


source to share


1 answer


This is a test with $ httpBackend and for mocking the service.



var describe = window.describe,
    beforeEach = window.beforeEach,
    afterEach = window.afterEach,
    it = window.it,
    expect = window.expect,
    inject = window.inject,
    module = window.module,
    angular = window.angular,
    serviceURL = '/' + Techsson.Core.Global.Language + '/api/sessionlimit/getdata',
    $scope,
    sessionLimitServiceResponse;

describe('Jasmine - SessionLimitService', function () {

    beforeEach(module('sessionlimit.module'));

    var sessionLimitServiceMock, q;

    beforeEach(inject(function (_SessionLimitService_, _SessionLimitResository_, $httpBackend, $rootScope) {
        sessionLimitServiceMock = _SessionLimitService_;
//remove the use of global variables
    $httpBackend.when('GET', serviceURL)
                            .respond('foo', {/*Headers*/});
        }));

    it("Content array must be empty", function () {
        expect(sessionLimitServiceMock.content.length).toEqual(0);
    });

    it('Content array must have a value', function() {
        $httpBackend.expectGET(serviceURL);
        sessionLimitServiceMock.getData().then(function(value) {
            expect(value).toEqual('foo'); // NOTHING HAPPENS
        });
        $httpBackend.flush();
        });
    });

      

+1


source







All Articles