Lev
Lev

Reputation: 15714

how can I verify a method was called n times?

I'm spying on a method of my component like this :

spyOn(component, 'someMethod');

How can I verify that component.someMethod was called n times ?

Intelisense is not giving me the calls attribute.

Upvotes: 0

Views: 1827

Answers (2)

TypeScripter
TypeScripter

Reputation: 909

You dont need any custom methods. Jasmine does provide method toHaveBeenCalledTimes()

See the jasmine doc.

https://jasmine.github.io/2.4/introduction.html

for your example it will be - expect(component.yourmethod).toHaveBeenCalledTimes(n);

Upvotes: 3

Amir Romashkin
Amir Romashkin

Reputation: 36

it("tracks the number of times it was called", function() {
    spyOn(foo, 'setBar');

    expect(foo.setBar.calls.count()).toEqual(0);

    foo.setBar();
    foo.setBar();

    expect(foo.setBar.calls.count()).toEqual(2);
  });

Upvotes: 1

Related Questions