用茉莉花大理石测试ngrx效果

时间:2019-02-22 10:02:20

标签: angular typescript jasmine ngrx

我具有以下相当标准的效果:

@Injectable()
export class ToDoEffects {

      @Effect()
      getToDoEntries$ = this.actions$.
        pipe(
          ofType(ToDoActionTypes.LoadToDoEntries),
          switchMap(_ => this.toDoService.loadToDoEntries()
            .pipe(
              tap(response => console.log(response)),
              map(response => new LoadToDoEntriesSuccess(response.body)),
              catchError(error => of(new LoadToDoEntriesError(error))
              ))
          ));

      constructor(private actions$: Actions, private toDoService: ToDoService) { }

如何使用jasmine-marbles进行测试?

目前,我有这个:

describe('ToDoEffects', () => {
  let actions$: Observable<any>;
  let effects: ToDoEffects;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientModule],
      providers: [
        ToDoEffects,
        provideMockActions(() => actions$),
      ]
    });

    effects = TestBed.get(ToDoEffects);
    actions$ = TestBed.get(Actions);

  });

  describe('getToDoEntries$', () => {
    it('should load to-do entries', () => {

      const mockEntries = [{
        begin: new Date('01.04.2011'),
        end: new Date('01.05.2011'),
        content: 'Something',
        priority: PRIORITY.LOW
      }, {
        begin: new Date('01.04.2011'),
        end: new Date('01.05.2011'),
        content: 'SomethingElse',
        priority: PRIORITY.LOW
      }] as ToDoEntry[];

      const action = new LoadToDoEntries();
      const completion = new LoadToDoEntriesSuccess(mockEntries);

      actions$ = hot('-a', { a: action });
      const response = cold('-a|', { a: mockEntries });
      const expected = cold('--c|', { c: completion });
      const toDoServiceSpy = jasmine.createSpyObj('ToDoService', ['loadToDoEntries']);
      toDoServiceSpy.loadToDoEntries.and.returnValue(response);

      expect(effects.getToDoEntries$).toBeObservable(expected);

    });
  });
});

但是-令人惊讶-它失败了。

我在collection.effects.spec.ts处或多或少地复制了此表格,但是我还不知道jasmine-marbles的工作方式,也不确定我是否正确嘲笑了该服务(注意:我不想使用jest)。

我正在使用Angular 7.2.3ngrx 7.2.0Jasmine 3.3.0jasmine-marbles 0.4.1

有人可以帮我写一个测试,让我入门吗?那我以它为基础。

0 个答案:

没有答案