writing jasmine unit test for angular factory

时间:2015-07-28 23:14:07

标签: javascript angularjs unit-testing jasmine chai

I am trying to test an angular factory using jasmine and mocha however I keep getting undefined errors.

here is my factory:

(function() {
    'use strict';

    angular.module('app').factory('appFactory', appFactory);

    appFactory.$inject = ['$http','$q'];

    function appFactory($http,$q) {
        return {
            get: get
        };

        function get(someData) {
            //....
        }
    }
})();

and here is my unit test using jasmine and mocha:

'use strict';

describe('Factory: appFactory', function() {
    var http, appFactory;
    var $rootScope, $scope;

    beforeEach(function() {
        angular.module('app');
    });

    beforeEach(inject(function($injector) {
        $rootScope = $injector.get('$rootScope');
        $scope = $rootScope.$new();
    }));

    beforeEach(inject(function() {
        var $injector = angular.injector(['ngMock', 'ng', 'app']);
        appFactory = $injector.get('appFactory');
    }));

    it('should be defined', inject( function() {
        alert(appFactory);
    }));
});

so my alert looks something like:

 Object{get: function get(someData) {...}

how do I go about testing the functionality of my factory if this is returning an object?

0 个答案:

没有答案