2017-08-03 38 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

相關問題