Grendizer
Grendizer

Reputation: 2213

Angularjs unit testing service with promise

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

Upvotes: 3

Views: 232

Answers (1)

Raulucco
Raulucco

Reputation: 3426

This is the 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();
        });
    });

Upvotes: 1

Related Questions