Sumit Khanduri
Sumit Khanduri

Reputation: 3798

Expected spy log to have been called

I have created a simple JS function which i want to test but when i run it is showing error Expected spy log to have been called what am i doing wrong ?

Function

function helloWorld() {
    console.log('hey');
}

Test spec

describe('Hello world', function () {
    it('says hello', function () {
        spyOn(console,'log').and.callThrough();
        expect(console.log).toHaveBeenCalled();
    });
});

Upvotes: 2

Views: 7959

Answers (1)

cas decelle
cas decelle

Reputation: 71

It's not an error, it's a failed test. That's because you don't call the log function in your example, so the test "correctly" fails.

describe('Hello world', function () {
  it('says hello', function () {
    spyOn(console,'log').and.callThrough();
    comp.helloWorld();
    expect(console.log).toHaveBeenCalled();
  });
});

Upvotes: 7

Related Questions