Angular2 Service Unit Test Fails with TypeError: undefined is not an object

Steve K

I am testing my authService. This is the full Test, yet Karma tells me, authService is undefined. I have plenty of Service which AuthService depends upon, but I provided and injected them all properly.

Error: Cannot resolve all parameters for 'AuthService'(BackendService, Store, LoggerService, undefined, ErrorService). Make sure that all the parameters are decorated with Inject or have valid type annotations and that 'AuthService' is decorated with Injectable. in /var/folders/zb/tpysrhsx7hbg1dnsn4gwtqq00000gn/T/8715f9a6c29e748f52c8f59e3e1daae3.browserify (line 34976)

authservice.spec.ts

import { provide } from "@angular/core";
import { AuthHttp } from "angular2-jwt";
import { HTTP_PROVIDERS, XHRBackend } from "@angular/http";
import { MockBackend } from "@angular/http/testing";
import {
    TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS,
    TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS
} from "@angular/platform-browser-dynamic/testing";
import {
    beforeEachProviders,
    inject,
    beforeEach,
    it,
    describe,
    setBaseTestProviders
} from "@angular/core/testing";
import { Subject } from "rxjs/Subject";
import { AuthService } from "./auth.service";
import { BackendService } from "../../backend/backend.service";
import { ErrorService } from "../../error/error.service";
import { LoggerService } from "../../logger/logger.service";
import { NavService } from "../../nav/nav-service/nav.service";
import { Store } from "@ngrx/store";
import { TestComponentBuilder } from "@angular/compiler/testing";
import { ToastController, AlertController } from "ionic-angular";
setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);

describe("AuthService", () => {

    let response = new Subject();
    let tcb;
    let authService;
    let navService;
    let backendService;
    let errorService;
    let store;
    let loggerService;

    class StubErrorService extends ErrorService {
        constructor() {
            super(null, null);
        }

        toast(title) {
            console.error(title);
        }

        modal(title, subtitle) {
            console.error(title, subtitle);
        }
    }

    class StubBackendService extends BackendService {

    }

    class StubStore extends Store<any> {

    }

    class StubLoggerService extends LoggerService {

    }

    class StubNavService extends NavService {

    }

    // PROVIDE

    beforeEachProviders(() => [
        HTTP_PROVIDERS,
        provide(AuthHttp, {
            useValue: {
                get: (url: string) => {
                    return response;
                }
            }
        }),
        AuthService,
        TestComponentBuilder,
        provide(ToastController, {useClass: null}),
        provide(AlertController, {useClass: null}),
        provide(ErrorService, {useClass: StubErrorService}),
        provide(XHRBackend, {useClass: MockBackend}),
        provide(BackendService, {useClass: StubBackendService}),
        provide(Store, {useClass: StubStore}),
        provide(LoggerService, {useClass: StubLoggerService}),
        provide(NavService, {useClass: StubNavService})
    ]);

    // INJECTS

    beforeEach(inject([TestComponentBuilder, AuthService, ErrorService, BackendService, Store, LoggerService, NavService], (_tcb, as, es, bs, s, ls, ns) => {
        tcb = _tcb;
        authService = as;
        navService = ns;
        errorService = es;
        store = s;
        backendService = bs;
        loggerService = ls;
    }));

    it("should test authservice", () => {
        authService.logout();
    });
});
sikac

I don't know if its relevant anymore, just to say I had an almost identical issue and I resolved it following the official docs on how to test services. Hope it helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular Unit test fails when i try to inject a service

From Dev

Angular Unit test fails when i try to inject a service

From Dev

TypeError: $scope is undefined in Angular controller unit test with Jasmine

From Dev

Instance initializer unit test fails with "store is undefined"

From Dev

Moq Unit test fails when calling a Service

From Dev

Moq Unit test fails when calling a Service

From Dev

Angular2 unit test + creating object of an interface

From Dev

Testing service with angular-mocks/jasmine - TypeError: undefined is not an object

From Dev

Testing service with angular-mocks/jasmine - TypeError: undefined is not an object

From Dev

'undefined is not an object' in karma/jasmine unit test

From Dev

Simple unit test for Angular service

From Dev

Angular unit test that calls a service

From Dev

Angular unit test that calls a service

From Dev

Angular Unit test for an injected service

From Dev

Unit test Angular 4 getting error `TypeError: Cannot read property 'subscribe' of undefined`

From Dev

Jasmine unit testing directives with external dependencies fails with TypeError: '[object Object]'

From Dev

AngularJS Jasmine test: TypeError: 'undefined' is not an object

From Dev

AngularJS TypeError: 'undefined' is not an object' Jasmine test

From Dev

How do you inject an angular2 service into a unit test? (RC3)

From Dev

Jasmine unit test fails when spying on a service method

From Dev

Jasmine unit test fails when spying on a service method

From Dev

Unit test fails

From Java

Angular2 unit test with @Input()

From Dev

Angular2 Inject ElementRef in unit test

From Dev

Angular2 unit test Click() with keypress

From Java

MatDialog Service Unit Test Angular 6 Error

From Dev

Cannot inject service into its Angular unit test

From Dev

Injecting a service into angular controller unit test

From Dev

Unit test failing on Angular service with promises

Related Related

  1. 1

    Angular Unit test fails when i try to inject a service

  2. 2

    Angular Unit test fails when i try to inject a service

  3. 3

    TypeError: $scope is undefined in Angular controller unit test with Jasmine

  4. 4

    Instance initializer unit test fails with "store is undefined"

  5. 5

    Moq Unit test fails when calling a Service

  6. 6

    Moq Unit test fails when calling a Service

  7. 7

    Angular2 unit test + creating object of an interface

  8. 8

    Testing service with angular-mocks/jasmine - TypeError: undefined is not an object

  9. 9

    Testing service with angular-mocks/jasmine - TypeError: undefined is not an object

  10. 10

    'undefined is not an object' in karma/jasmine unit test

  11. 11

    Simple unit test for Angular service

  12. 12

    Angular unit test that calls a service

  13. 13

    Angular unit test that calls a service

  14. 14

    Angular Unit test for an injected service

  15. 15

    Unit test Angular 4 getting error `TypeError: Cannot read property 'subscribe' of undefined`

  16. 16

    Jasmine unit testing directives with external dependencies fails with TypeError: '[object Object]'

  17. 17

    AngularJS Jasmine test: TypeError: 'undefined' is not an object

  18. 18

    AngularJS TypeError: 'undefined' is not an object' Jasmine test

  19. 19

    How do you inject an angular2 service into a unit test? (RC3)

  20. 20

    Jasmine unit test fails when spying on a service method

  21. 21

    Jasmine unit test fails when spying on a service method

  22. 22

    Unit test fails

  23. 23

    Angular2 unit test with @Input()

  24. 24

    Angular2 Inject ElementRef in unit test

  25. 25

    Angular2 unit test Click() with keypress

  26. 26

    MatDialog Service Unit Test Angular 6 Error

  27. 27

    Cannot inject service into its Angular unit test

  28. 28

    Injecting a service into angular controller unit test

  29. 29

    Unit test failing on Angular service with promises

HotTag

Archive