首页 > 解决方案 > 错误:StaticInjectorError(DynamicTestModule)[NotificationsComponent

问题描述

运行时完全错误ng test

错误:StaticInjectorError(DynamicTestModule)[NotificationsComponent

AuthService]:StaticInjectorError(平台:核心)[NotificationsComponent AuthService]: 

 NullInjectorError:没有 AuthService 的提供者!

预计未定义是真实的。

错误:预期未定义是真实的。

    在堆栈(http://localhost:9876/base/node_modules/jasmine-core/lib/jasmine-core/jasmine.js:2176:17

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

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

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

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

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

    在对象。(http://localhost:9876/_karma_webpack_/webpack:/C:/../webTest/src/app/components/notifications/notifications.component.spec.ts:97:23

    在 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 )

    在 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 )

    在 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 )

我的代码服务:

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


    let component: AuthService;

    let fixture: ComponentFixture<AuthService>;


    beforeEach(() => {

        TestBed.configureTestingModule({

            declarations: [AuthService]

        })

        fixture = TestBed.createComponent(AuthService);

        component = fixture.componentInstance;

    });

});

你能问我,有什么问题吗?

标签: angularunit-testingtypescripttesting

解决方案


像下面这样使用它:

服务应该在providers数组中。

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;
  });

});

推荐阅读