Question

I am using jasmine runner to test angular code.

describe('des1', function() {
  var des1Var = function(){};
  beforeEach() {
    //....
  }

  describe('test1', function() {
    var scope4Compile = $rootScope.$new();
    var des2Var = des1Var(scope4Compile); // returns undefined.

    beforeEach(function() {
      des2Var = des1Var(scope4Compile); // returns des1Var() fine;
    })

    it('should do ', function(){
      //should do...
    })

    it('should also do', function(){
      //should also do...
    })
  })
})

I need to instantiate something once before the it statements, if run multiple times result is pretty bad. How can I get it done properly?

Was it helpful?

Solution

I believe it you call it once in the first beforeEach it will be run one time for each describe that is below it.

In the code below, des2Var will be set once for the whole test1 describe.

describe('des1', function() {
  var des1Var = function () { };
  beforeEach(function () {
    var des2Var = des1Var();
  });

  describe('test1', function() {
    it('should do ', function(){
        //should do...
    });

    it('should also do', function(){
        //should also do...
    });
  });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top