user9794198
user9794198

Reputation:

Error: StaticInjectorError(DynamicTestModule)[NotificationsComponent

Full Error when run ng test:

Error: StaticInjectorError(DynamicTestModule)[NotificationsComponent

AuthService]:    StaticInjectorError(Platform: core)[NotificationsComponent AuthService]: 

 NullInjectorError: No provider for AuthService!

Expected undefined to be truthy.

Error: Expected undefined to be truthy.

    at stack (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2176:17)

    at buildExpectationResult (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2146:14)

    at Spec.expectationResultFactory (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:766:18)

    at Spec.addExpectationResult (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:444:34)

    at Expectation.addExpectationResult (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:710:21)

    at Expectation.toBeTruthy (http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2099:12)

    at Object. (http://localhost:9876/_karma_webpack_/webpack:/C:/../webTest/src/app/components/notifications/notifications.component.spec.ts:97:23)

    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/C:/C:/../webTest/node_modules/zone.js/dist/zone.js:388:1)

    at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/C:/../webTest/node_modules/zone.js/dist/proxy.js:79:1)

    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/C:/../webTest/node_modules/zone.js/dist/zone.js:387:1)

my code service:

describe('Component: Auth', () => {


    let component: AuthService;

    let fixture: ComponentFixture<AuthService>;


    beforeEach(() => {

        TestBed.configureTestingModule({

            declarations: [AuthService]

        })

        fixture = TestBed.createComponent(AuthService);

        component = fixture.componentInstance;

    });

});

Can you ask me, what is the problem?

Upvotes: 3

Views: 27626

Answers (2)

Sangwin Gawande
Sangwin Gawande

Reputation: 8176

Use it like below :

Services should be in providers array.

describe('Component: Auth', () => {
let component: AuthService;
let fixture: ComponentFixture<AuthService>;

  beforeEach(() => {
    TestBed.configureTestingModule({
        declarations: [],
        providers: [AuthService] // **Like this.**
    })
    fixture = TestBed.createComponent(AuthService);
    component = fixture.componentInstance;
  });

});

Upvotes: 7

jacekbj
jacekbj

Reputation: 631

You have to instantiate service: let service = new AuthService();, there is no need to call "createComponent" and such.

https://angular.io/guide/testing#service-tests

Upvotes: 0

Related Questions