vendredi 5 février 2016

what and how to do unit test for angular factories

Angular factory is great!! While writing unit test, it is kind of confusing, should we write unit test or not.

I have following factory:

(function(myApp) {
myApp.factory('MyFirstFactory', function(MySecondFactory){
  function MyFirstFactory(config){
    this.value1 = 'value1';
    setDefault(this);
  }

   MyFirstFactory.doSomething = function(){
     var config = {
       findWork: true;
       myWork: MySecondFactory.doWork()
     };
     return new MyFirstFactory(config);
   };

  return MyFirstFactory;
});
})(angular.module('MyModule'));

So for above factory, do we need to write unit test for MyFirstFacotry and doSomething function? If yes, how can we achieve using jasmine and karma.. Tried following:

describe('MyFirstFactory', function() {
   var mockMyFirstFactory;
   beforeEach(function(){
     module('MyModule');
     inject(function(MyFirstFactory){
        mockMyFirstFactory = MyFirstFactory;
     });
   });

  it('MyFirstFactory should be defined', function(){
     expect(mockMyFirstFactory).toBeDefined();
  });
  it('should do something', function(){
    // how to test doSomething 
  });
});

I am using jasmine, angular-mock, karma.

Aucun commentaire:

Enregistrer un commentaire