1

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 pass values to testVar
      // testVar runs before I can assign value to it.
      // even if I have setters and getters how can I retest the if statement
   });
});
..

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

      if(testVar ){
        // want to test this code. has 10 line of code
      }
      ...  
      function foo(){
        //does something and I can test this
      }
      ...
      return {
         foo:foo
      }
  }
})();

how do i assign values to testVar before the if statement runs

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 in the first place. Thanks

2
  • It depends on what you have inside if (testVar) { Commented Sep 9, 2016 at 14:50
  • @jcubic sry for confusion, I actually want to be able to pass a value to testVar so that code inside can be tested. Commented Sep 9, 2016 at 14:53

2 Answers 2

1

you need to inject someOtherFile (which is, if I understand correctly a Service too) into fooFactory when creating it.

So have something like this in your test if you want to completly mock someOtherFile

describe('test fooFactory', function(){
    var fooFactory;
    beforeEach(function(){
        fooFactory = new FooFactory(
            { someOtherfunc: function() { return true; } }
        );
        stateChangeCallback = $rootScope.$on.calls.first().args[1];
    });

    it('test if statement', function(){
       expect(fooFactory).toBe(?);
       // how to write a test to pass values to testVar
       // testVar runs before I can assign value to it.
       // even if I have setters and getters how can I retest the if statement
    });
});

However, if you need someOtherFile and you don't want to mock all its responses, what you can do is use angular dependancy injection to inject this service and then only mock someOtherfunc on it. That will give something like this:

describe('test fooFactory', function(){
    var fooFactory;
    var someOtherFile;

    beforeEach(inject(function (
        _someOtherFile_
    ) {
        someOtherFile = _someOtherFile_;
        fooFactory = new FooFactory(
            someOtherFile
        );
    }));

    it('test if statement', function(){
       spyOn(someOtherFile, 'someOtherfunc').and.returnValue(true);
       expect(?).toBe(?);
       // how to write a test to pass values to testVar
       // testVar runs before I can assign value to it.
       // even if I have setters and getters how can I retest the if statement
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

thanks, this would make sense if I care abt the value I get from someOtherfunc, but what if I dont, and just want to set the value of 'testVar' to some random value. Do you think in that case I need to have setter and call it in beforeEach?
I don't get it, if testVar is not equal to true you cannot test your code inside the if no ?
1

You cannot test functions/variable that are not accessible outside your factory.

The proper way of doing it would be to expose it. But be aware that you should not be exposing everything just to make it testable. You should really consider if adding a test for that function/variable will actually add value to your application.

2 Comments

I agree, if it doesnt make sense then probably its not needed .
That's the spirit! (Y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.