vendredi 9 septembre 2016

javascript unit testing variables and code not encapsulated inside functions

how to write unit test for variables in a angular js file.

fooFactory.spec.js
..
describe('test fooFactory', function(){
   it('test if statement', function(){
      expect(?).toBe(?);
      // how to write a test to inspect code inside if statement testVar
      // testVar runs before I can assign value to it.
   });
});
..

fooFactory.js
(function () {
   angular.module('MyApp').factory('fooFactory', fooFactory);
   function fooFactory(someOtherFile){
      var testVar = someOtherFile.someOtherfunc;

      if(testVar ){
        // how do I test this code?
      }
      ...  
      function foo(){
        //does something
      }
      ...
      return {
         foo:foo
      }
  }
})();

how do i test the if statement

if(testVar ){
     // how do I test this code?
  }

Should I encapsulate the entire if in a function and pass it through the return.

  bar();
  function bar(data){
     if(data){
        testVar = data;
     }
     if(testVar ){
        // how do I test this code?
     }
  }
  return {
   foo: foo,
   bar: bar
  }

Is there a better way to do this. Or should the js file have setters and getters. Thanks

Aucun commentaire:

Enregistrer un commentaire