2017-05-09 7 views
1

私は下記のように私のアプリケーションにストアを使用しています。Mock @ ngrx /アングル2の店

export class NavigationComponent { 
    navigationLinks$: Observable<Navigation[]>; 

    constructor(private store: Store<State>) { 
    this.navigationLinks$ = this.store.select('navigation') 
           .map((result: State) => result.navigationLinks); 
    } 

ここで、単体テストを作成しようとしていますが、このストアを模擬したいと考えています。

1. this.store.selectが(「」)と呼ばれたときモックデータを返します模擬店を作成するモックストア の作成:これは私がやっているものです。 mockdataは、navigationLinksという配列型のプロパティを返します。

class StoreMock { 
    public dispatch(obj) { 
    console.log('dispatching from the mock store!') 
    } 

    public select(obj) { 
    console.log('selecting from the mock store!'); 

    return Observable.of([ 
     { 'navigaitonLinks$': [{ 'name': 'Help', hasChild: false}] } 
    ]) 
    } 
} 

2. BeforeEachブロック

describe('NavigationComponent',() => { 
    let component: NavigationComponent; 
    let fixture: ComponentFixture<NavigationComponent>; 

    beforeEach(async(() => { 
    TestBed.configureTestingModule({ 
     declarations: [NavigationComponent], 
     providers: [{provide: Store, useClass: StoreMock}], 
     imports: [ 
     StoreModule.provideStore(reducers) 
     ], 
    }) 
     .compileComponents(); 
    })); 

beforeEach(() => { 
    fixture = TestBed.createComponent(NavigationComponent); 
    component = fixture.componentInstance; 
    fixture.detectChanges(); 
    }); 

3.私はこのテストは期待の文ごとに失敗します知っているが、私はnavigationLinks $プロパティを移入することはできませんよ 私のテスト私の模擬データ。

it(`should create the navigation with 'Help' link`,() => { 

    let navLinks: any[]; 
    component.navigationLinks$.subscribe(); 
    console.log(navLinks); // This should print my mockdata so i can assert it 
    expect(component.navigationLinks$).toEqual('Help'); 
    }); 

console.logは未定義を出力し、MockStoreのselect()が返すデータを読み取ることができません。余計なことはありますか?

答えて

1

私は同じ問題があり、Observable.of()関数でオブジェクトを返すだけです。

it(`should create the navigation with 'Help' link`,() => { 
    component.navigationLinks$.subscribe((links) => { 
    console.log(links); // This should print an array of Links 
    }); 
}); 

return Observable.of([ 
    { 'navigaitonLinks$': [{ 'name': 'Help', hasChild: false}] } 
]) 

return Observable.of([{ 'name': 'Help', hasChild: false}, {}, {} ... ]); 

にこれはあなたの被監視オブジェクトを移入します

関連する問題