mardi 28 juillet 2015

writing jasmine unit test for angular factory

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?

Aucun commentaire:

Enregistrer un commentaire