【问题标题】:Angular Testing: fails to resolve all parameters for Store: (?, ?, ?)角度测试:无法解析商店的所有参数:(?,?,?)
【发布时间】:2017-08-03 19:55:58
【问题描述】:

我正在尝试将我的 ngrx/Store 声明到规范文件中以测试容器组件。所以我首先导入商店,然后在 beforeEach 中执行以下操作:

      import {Store} from '@ngrx/store';

       beforeEach(async(() => { 
        TestBed.configureTestingModule({
              declarations: [NotificationsComponent, NotificationsDevicesComponent, 
               ToArrayPipe, OrderByDate, TimezoneFormatPipe],
              providers: [{ provide: Store, useClass: Store }]
            })
              .compileComponents().then(() => {
                fixture = TestBed.createComponent(NotificationsComponent);
                component = fixture.componentInstance;
                el = fixture.debugElement;

                fixture.detectChanges();
              });
          }));

但我从标题中得到错误,无法解析 Store 的所有参数:(?, ?, ?)。

有什么想法吗?

非常感谢

【问题讨论】:

  • cannot resolve parameters for foo: (?, ?) 通常意味着您的依赖注入无法正常工作(循环依赖、错误导入、错误导出等)。问号将指示依赖关系。有时它会像(FooService, BarService, ?),这意味着第三个依赖是有问题的。
  • 尝试将您尝试注入的 3 项服务直接从他们的文件中导入 Store,而不是从桶中,如果您正在这样做的话。

标签: angular ngrx ngrx-store


【解决方案1】:

我用过ngrx并测试过store

这是我遵循的步骤 -

  1. 导入StoreModuleStore

    import { StoreModule, Store } from '@ngrx/store';
    
  2. 导入我的减速器

    import { reducers } from './reducers';
    
  3. 在我的TestBed 中添加了StoreModule

    TestBed.configureTestingModule({
      imports: [
        StoreModule.forRoot({
          categories: reducers // My reducers
        })
      ],
    
  4. 定义store变量

    let store: Store<CategoryState>;
    
  5. 初始化store

    beforeEach(() => {
      store = TestBed.get(Store);
    
      spyOn(store, 'dispatch').and.callThrough();
    
      fixture = TestBed.createComponent(CategoryListComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
    
  6. 之后,您可以在测试用例中使用store

【讨论】:

  • 错误消失了,尽管我需要用StoreModule.provideStore({}) 替换StoreModule.forRoot({}),因为StoreModule 似乎没有forRoot 方法。另外,您知道为什么 angular 通常会引发随机错误吗?在测试任何东西之前就像TypeError: this.store.map is not a function?非常感谢
  • 您使用的是旧版本的@ngrx。在最新版本中,他们已将provideStore 替换为forRoot。这不是随机错误。在 angular 2 中,错误已被简化。例如,如果说this.store.map 不是函数。然后你必须查看你的store。它不包含正确的值。
  • 谢谢,我最终升级了所有东西,它似乎可以部分工作。你知道吗,虽然我为什么会得到类似的错误:TypeError: Cannot read property 'X' of undefined 其中undefined 基本上是this.XMap$ = store.select(state =&gt; state.storeData.X);
  • 您的store.select 可能无法正常工作。您可以使用selectorsstore 中选择数据。 github.com/ngrx/platform/blob/master/docs/store/selectors.md 是如何使用选择器的链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 2019-03-16
  • 1970-01-01
相关资源
最近更新 更多