Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67497

Angular Test a function that updates a value

$
0
0

I would like to test this function that only updates a Boolean variable. But when I test with toHaveBeenCalled method it passes because the function has been called. However when I read the value of the variable it never updates even though the function was successfully called.

See the code below for more details, the first portion is the main method and the second is the test.

// Main Code.
variableToUpdate: boolean = false;
updateVariable(){
  this.variableToUpdate = true;
}

// Passing Test
it('should call the function', () => {
  spyOn(component, 'updateVariable');
  component.updateVariable();
  expect(component.updateVariable).ToHaveBeenCalled();
});

// Failing Test
it('should update the value of the variable', () => {
  spyOn(component, 'updateVariable');
  component.updateVariable();
  expect(component.variableToUpdate).ToEqual(true);
});

Viewing all articles
Browse latest Browse all 67497

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>