Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jasmine - How to spy on a function call within a function?

Tags:

The following is in my controller:

$scope.addRangesAndSquare = function() {     $scope.addLeftRange();     $scope.addCenterSquare();     $scope.addRightRange(); } 

And I want to spy on $scope.addLeftRange(), so that when $scope.addRangesAndSquare is called so is $scope.addLeftRange():

it('expect addLeftRange to be called after calling addRangesAndSquare', function () {     spyOn(scope ,'addRangesAndSquare');     spyOn(scope, 'addLeftRange');     scope.addRangesAndSquare();     expect(scope.addLeftRange).toHaveBeenCalled(); }); 

How can this be done?

like image 477
criver.to Avatar asked Jul 17 '13 16:07

criver.to


People also ask

How do you spy a function in Jasmine?

SpyJasmineSpec.js describe("Example Of jasmine Spy using Create Spy", function() { it("can have a spy function", function() { var person = new Person(); person. getName11 = jasmine. createSpy("Name spy"); person.

How do you spy on a variable in Jasmine?

In Jasmine, you can do anything with a property spy that you can do with a function spy, but you may need to use different syntax. Use spyOnProperty to create either a getter or setter spy. it("allows you to create spies for either type", function() { spyOnProperty(someObject, "myValue", "get").

Does spyOn call function?

javascript - Jest spyOn() calls the actual function instead of the mocked - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

What is the use of callThrough in Jasmine?

From Jasmine doc: By chaining the spy with and. callThrough, the spy will still track all calls to it but in addition it will delegate to the actual implementation.


1 Answers

By default, when you use spyOn with jasmine, it mocks that function and doesn't actually execute anything within it. If you want to test further function calls within, you'll need to call .andCallThrough(), like so:

spyOn($scope, 'addRangesAndSquare').andCallThrough(); 

that should do it.

like image 90
UnicodeSnowman Avatar answered Sep 25 '22 04:09

UnicodeSnowman