如何对这个文件进行单元测试?

时间:2017-05-24 14:24:35

标签: jasmine ngrx

我想对这个文件进行单元测试,请告诉我如何为此编写茉莉花测试

import { Effect, Actions } from '@ngrx/effects';
import { NavigationActionType } from 'app/core/store/actions/navigation/navigation.action';
import { NavigationService } from 'app/core/services/navigation.service';
import 'rxjs/add/operator/switchMap';

@Injectable()
export class NavigationServiceEffect {
    @Effect()
    navigations$ = this.actions$
                        .ofType(NavigationActionType.TRIGGER_LOAD_NAVIGATION_ITEMS)
                        .switchMap( () => this.navigationService.load())
                        .map(data => ({ type: NavigationActionType.LOAD_NAVIGATIONS_LINKS, payload: data}));

    constructor(private actions$: Actions, private navigationService: NavigationService) {}

}

2 个答案:

答案 0 :(得分:0)

it('should dispatch LOAD_NAVIGATIONS_LINKS', () => {
  runner.queue({
    type: NavigationActionType.TRIGGER_LOAD_NAVIGATION_ITEMS,
    payload: 1
  });

  navigationServiceEffect.navigations$.subscribe(result => {
    expect(result.type).toEqual(NavigationActionType.LOAD_NAVIGATIONS_LINKS)
    expect(result.payload).toEqual(1);
  });
}

请注意,您必须模拟navigationService

答案 1 :(得分:0)

您可以使用提供test_transform()import logging logging.basicConfig(level=logging.DEBUG) def test_transform() ... 观测值的jasmine-marbles来测试这种效果:

hot