2017-08-03 35 views
0

我试图将我的ngrx/Store声明为测试容器组件的spec文件。所以我导入存储第一和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(); 
       }); 
      })); 

但我从标题中的错误不能解决店铺的所有参数:(,,???)。

任何想法?

非常感谢

+0

'无法解析foo参数:(?,?)'通常意味着您的依赖注入工作不正常(循环依赖,坏导入,坏导出等)。问号将表明依赖关系。有时它会像'(FooService,BarService,?)',这意味着第三个依赖关系就是问题所在。 – Lansana

+0

尝试将他们想要注入的3个服务直接从文件中导入到Store中,而不是从桶中导入,如果这就是您正在做的。 – Lansana

回答

0

我已经使用ngrx和测试store

下面是我遵循的步骤 -

  1. 进口StoreModuleStore

    import { StoreModule, Store } from '@ngrx/store'; 
    
  2. 进口减速机我

    import { reducers } from './reducers'; 
    
  3. 新增StoreModuleTestBed

    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

+0

错误已经消失,尽管我需要用StoreModule.provideStore({})替换StoreModule.forRoot({}),因为它看起来似乎并不是StoreModule具有'forRoot'方法。另外,你知道为什么角度抛出一般的随机错误?就像'TypeError:this.store.map在测试任何东西之前不是一个函数?非常感谢 – KevinOrfas

+0

您正在使用'@ ngrx'的旧版本。在最新版本中,他们用'forRoot'替换'provideStore'。这不是一个随机错误。在角度2中,错误已被简化。例如,如果说'this.store.map'不是一个函数。然后你必须看看你的'商店'。它不包含正确的值。 –

+0

谢谢你,我最终升级了一切,似乎部分工作。你知道,虽然为什么我得到这样的错误:'TypeError:无法读取undefined'的属性'X',其中'undefined'基本上是'this.XMap $ = store.select(state => state.storeData.X); ' – KevinOrfas

相关问题