简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM