首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误: StaticInjectorError(DynamicTestModule)[NotificationsComponent

错误: StaticInjectorError(DynamicTestModule)[NotificationsComponent
EN

Stack Overflow用户
提问于 2018-05-21 12:57:39
回答 2查看 27.3K关注 0票数 3

运行ng test时完全错误

错误: StaticInjectorError(DynamicTestModule)NotificationsComponent StaticInjectorError(平台:核心)NotificationsComponent授权服务 没有提供AuthService的提供者! 被认为是真实的。 错误:未定义为真实的预期。 在堆栈(modules/jasmine-core/lib/jasmine-core/jasmine.js:2176:17) buildExpectationResult (modules/jasmine-core/lib/jasmine-core/jasmine.js:2146:14) Spec.expectationResultFactory (modules/jasmine-core/lib/jasmine-core/jasmine.js:766:18) Spec.addExpectationResult (modules/jasmine-core/lib/jasmine-core/jasmine.js:444:34) Expectation.addExpectationResult (modules/jasmine-core/lib/jasmine-core/jasmine.js:710:21) Expectation.toBeTruthy (modules/jasmine-core/lib/jasmine-core/jasmine.js:2099:12) 在物体上。(/webpack:/C:/../webTest/src/app/components/notifications/notifications.component.spec.ts:97:23) ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (modules/zone.js/dist/zone.js:388:1) ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke (modules/zone.js/dist/proxy.js:79:1) ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke (modules/zone.js/dist/zone.js:387:1)

我的代码服务:

代码语言:javascript
复制
describe('Component: Auth', () => {


    let component: AuthService;

    let fixture: ComponentFixture<AuthService>;


    beforeEach(() => {

        TestBed.configureTestingModule({

            declarations: [AuthService]

        })

        fixture = TestBed.createComponent(AuthService);

        component = fixture.componentInstance;

    });

});

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

EN

回答 2

Stack Overflow用户

发布于 2018-05-21 13:04:29

如下所示使用它:

服务应该在providers数组中。

代码语言:javascript
复制
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;
  });

});
票数 7
EN

Stack Overflow用户

发布于 2018-05-21 13:05:48

您必须实例化服务:let service = new AuthService();,不需要调用"createComponent“等等。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50449449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档