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:
Here is the similar scenario of app in plnkr.
Idk, if I am asking too much. Thanks in advance.
The key principles are:
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();
    });
});
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With