Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine unit test AngularJS factory with two dependencies ($http and another factory returning promise)

I am using Ionic framework for custom applications. In the process, I am trying to write Unit test for the factory datastoreServices which has a dependency on DomainService and $http. I am kind a confused on the implementation of Jasmine Unit tests.

My factories are as follows.

app.factory("datastoreServices", ["$http", function($http) {
    return {
        getData: function(data, DomainService) {
            return $http.post(DomainService.host + 'factor', data);
        }
    };
}]);

app.factory('DomainService', function() { //here
    if (ionic.Platform.isAndroid()) {
        return {
            host: 'http://10.0.2.2:7001/'
        }
    }
    return {
        host: 'http://localhost:7001/'
    }
})

And my unit test skeleton is as follows. It has two dependencies so, couldn't figure out how to proceed. This is what I got so far for in unit test file.

describe(
        'datastoreServices',
        function() {
            beforeEach(module('Myapp'));
            describe('getData'),
                function() {
                    it("Should return correct values", inject(function(datastoreServices, DomainService, $httpBackend) {
                            expect(datastoreServices.getData(httpBackend.. /***something here!**/ )
                                .toEqual("2.2");
                            }))
                    }

I have very little knowledge on mocking and stuffs. Can someone help me test that factory datastoreServices. The following things are to be tested:

  • Is Http post making correct calls?
  • Is the function returning correct promise?

Here is the similar scenario of app in plnkr.

Idk, if I am asking too much. Thanks in advance.

like image 334
Dinesh Devkota Avatar asked Jul 27 '15 18:07

Dinesh Devkota


1 Answers

The key principles are:

  • $http is mocked during testing, meaning that your server is not being actually called during your test execution
  • you must use $httpBackend in order to assert http calls and mock server response https://docs.angularjs.org/api/ngMock/service/$httpBackend
  • you can easily inject or mock any dependencies needed for your test

Here's an example based on your OP code:

    describe('datastoreServices', function() {

    beforeEach(module('MyApp'));

    // get a reference to the $httpBackend mock and to the service to test, and create a mock for DomainService
    var $httpBackend, datastoreServices, DomainService;
    beforeEach(inject(function(_$httpBackend_, _datastoreServices_) {
        $httpBackend = _$httpBackend_;
        datastoreServices = _datastoreServices_;
        DomainService = function() {
            return {
                host: 'http://localhost:7001/'
            };
        };
    }));

    // after each test, this ensure that every expected http calls have been realized and only them
    afterEach(function() {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });


    it('calls http backend to get data', function() {
        var data = {foo: 'bar'};

        // write $http expectation and specify a mocked server response for the request
        // see https://docs.angularjs.org/api/ngMock/service/$httpBackend
        $httpBackend.expectPOST('http://localhost:7001/factor', data).respond(201, {bar: 'foo'});

        var returnedData;
        datastoreServices.getData(data, DomainService).success(function(result) {
            // check that returned result contains
            returnedData = result;
            expect(returnedData).toEqual({bar: 'foo'});
        });

        // simulate server response
        $httpBackend.flush();

        // check that success handler has been called
        expect(returnedData).toBeDefined();
    });
});
like image 128
Hugo G. Avatar answered Sep 30 '22 18:09

Hugo G.