Javier G.
Javier G.

Reputation: 43

Not able to increase code coverage in Angular Unit testing

I am trying to learn how to make unit testing for Angular with Karma and Jasmine.

My problem is that I make it with spyOn() and expect.toHaveBeenCalled() and even though Karma says that passed the tests, the Code Coverage is not updated.

I am new to angular unit testing and I dont know how to test methods properly so as to get code coverage.

Thanks everyone for the help.

Upvotes: 2

Views: 8436

Answers (2)

Tatsiana
Tatsiana

Reputation: 59

Actually you need not just create a spy but also execute it to increase coverage. A spy replaces the spied function with a stub. If you want this spy function to be called normally, you need to add .and.callThrough() to your spy.

spyOn(EnvironmentService, 'isProduction').and.callThrough()

You can also read more here: about spy and callThrough

Upvotes: 2

Andrei
Andrei

Reputation: 12206

spyOn(EnvironmentService, 'isProduction'); overwrites the service method and then EnvironmentService.isProduction(); is calling a spy instead of your method. Thus your method isn't called. valid test would be removing spy and will looke in some way like expect(EnvironmentService.isProduction()).toBe(false)

Upvotes: 1

Related Questions