错误:StaticInjectorError(DynamicTestModule)[NotificationsComponent

2 testing unit-testing typescript angular

运行时出现完全错误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;

    });

});
Run Code Online (Sandbox Code Playgroud)

你能问我,这是什么问题?

San*_*nde 7

如下使用:

服务应该是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;
  });

});
Run Code Online (Sandbox Code Playgroud)