Search code examples
angularunit-testingjasminekarma-jasmineangular-unit-test

Angular - Karma unit test not working, why?


As the title suggests, I'd like to ask if anyone knows the solution to how to get this unit test to pass, after adding the support service to the test123 component, it won't display true anymore no matter what I do. Image sample of the code and error messages


Solution

  • In the future, please post code as code, not as an image.

    There are a number of issues with the code as you outlined. Here are a few:

    • You showed us the service function secondsToTimeFormat(), but in the component you call this.supportService.secondsToTime().
    • in the component you call that same function with what appears to be an object, without the curly braces.
    • you do not provide for SupportService in your providers array of the TestBed in your .spec file.
    • Since SupportService is not mocked, it tries to call the real service, which resulted in the error you saw of no provider for Router!. In order to isolate this component, you ought to mock the service call with a spy.

    To show you all this actually working, I put together this Stackblitz.

    See the docs for details on how to do all this.