lundi 27 juillet 2015

Jasmine unit test AngularJS factory with Dependencies

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. Idk, if I am asking too much. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire