Angular-业力-ngrx-无商店提供者 [英] Angular - Karma - ngrx - No provider for Store

查看:64
本文介绍了Angular-业力-ngrx-无商店提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一项单元测试中,我正在尝试模拟@ ngrx/store.我已经在另一个规格文件中成功使用了该技术,但是当在此规格文件中尝试使用该技术时,出现了注入错误,提示No provider for Store!以下是规格文件中的相关代码:

In one of my unit tests, I'm trying to mock @ngrx/store. I've used the technique successfully in another spec file, but when trying to use it in this one, I'm getting an injection error saying No provider for Store! Below is the relevant code from the spec file:

beforeEach(async(() => {
  const emptyState = { opportunities: { list: { items: [], page: 1, total: 0 } } };
  const mockStore = new MockStore<MockAppState>(emptyState);

  TestBed.configureTestingModule({
    declarations: [
      OpportunityListComponent,
      FilledArrayPipe
    ],
    imports: [
      NgFilterListModule,
      FormsModule
    ],
    providers: [
      { provide: OpportunityApi, useValue: apiStub },
      { provide: Store, useValue: mockStore },
      { provide: Router, useValue: routerStub }
    ]
  }).compileComponents();
}));

beforeEach(() => {
  store = fixture.debugElement.injector.get('Store');
});

此组件与成功使用MockStore类的组件之间的唯一区别是,此组件被延迟加载到与AppModule分开的其自己的模块中.但是,我尝试在该模块中导入StoreModule以及在TestBed导入中包括StoreModule,都无济于事.

The only difference between this component and the one that successfully uses the MockStore class is that this component is lazy loaded in its own module separate from AppModule. However, I tried importing StoreModule in that module as well as including StoreModule in the TestBed imports, both to no avail.

推荐答案

原来我的问题是我在fixture.debugElement.injector.get('Store')调用中引用了Store.删除引号解决了我的问题.

Turns out my problem was I quoting Store in the fixture.debugElement.injector.get('Store') call. Removing the quotes fixed my problem.

这篇关于Angular-业力-ngrx-无商店提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆