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?
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.
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").
javascript - Jest spyOn() calls the actual function instead of the mocked - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With