3

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!

2
  • I don't understand why you use promises on your test. I would just return the value on the getData spy. On test angular does not call $scope.$digest() this must be call manually when using $httpBackend module. I suggest you use $httpBackend to mock the ajax call and then $scope.digest Commented Jun 29, 2015 at 11:33
  • The above comment is wrong i confused the digest cycle with the flush method of $httpBackend. Anyway when testing a directive ort a controller might be helpfull to use the digest method of the $scope object Commented Jun 29, 2015 at 13:20

1 Answer 1

1

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();
        });
    });
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply. I get value as undefined in the above example. Pls see the edit above.
Yep, right it is $httpBackend.flush() instead of $scope.$digest() according to docs. I'll update the example
Hi again! I'm still getting the value as undefined...I updated the code above again...
I updated the code . I forgot to use $httpBackend.expectGET. It is good to add an afterEach too as on the docs: ` afterEach(function() { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); });`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.