Using Jasmine to Spy on Variables in a Function

Suppose I have a function

function fun1(a) {
  var local_a = a;
  local_a += 5;
  return local_a/2;
}

      

Is there a way to check if the local_a value is what it should be (like in the first line of code)? I'm a bit new to Jasmine so I'm stuck. Thanks in advance.

+15


source to share


1 answer


Not really. You can do the following though:

Check the result fun1()

:

expect(fun1(5)).toEqual(5);

      

Make sure it is actually called (useful if it happens via events) and also check the result:



var spy = jasmine.createSpy(window, 'fun1').andCallThrough();
fire_event_calling_fun1();
expect(spy).toHaveBeenCalled();
expect(some_condition);

      

Actually reproduce the whole function that checks intermediate results:

var spy = jasmine.createSpy(window, 'fun1').andCallFake(function (a) {
  var local_a = a;
  expect(local_a).toEqual(a);
  local_a += 5;
  expect(local_a).toEqual(a+5);
  return local_a/2;
});
fun1(42);
expect(spy).toHaveBeenCalled();

      

+15


source







All Articles