Aaron
Aaron

Reputation: 1455

Jasmine - Method not being called

var obj = {};

obj.localContext = 'firstTemp';

  obj.call = function(){
    obj.localContext = 'secondTemp';
  };

Jasmine-spec:

it('value of localContext', function(){
  spyOn(obj, 'call');
  obj.call();
  expect(obj.localContext).toEqual('secondTemp');
});

Why is the obj.call() method never being called? When I run the spec, the value of obj.localContext is still firstTemp instead of secondTemp

Upvotes: 1

Views: 2004

Answers (1)

SpoonMeiser
SpoonMeiser

Reputation: 20417

When you create a spy, the default behaviour is to replace the object with a mock that doesn't call the original. Typically you'd use it to test functionality that would otherwise call out to other APIs you don't want to be called - you can test that they would have been called, without actually calling them.

Jasmine does provide you a way to also call the original function though:

spyOn(obj, "call").and.callThrough();

See the Jasmine documentation for spies (unfortunately, linking directly to the and.callThrough section doesn't work)

Upvotes: 3

Related Questions