Skip to content

When testing effects they are fired twice, which breaks HttpTestingController #551

@martinroob

Description

@martinroob

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request

What is the current behavior?

I am trying to test an effect following the way described in the documentation.
The effect calls a backend system via HttpClient and I want to test this by using angulars HttpTestingController. But it seams that the effect is called 2 times and the (mock) backend is called 2 times too.

The effect looks like this:


  @Effect({dispatch: false})
  readData$: Observable = this.actions$
    .ofType(READ)
    .switchMap((action: ReadAction) => {
      return this.httpClient.get('/example/data/' + action.payload)
        .map((response: any) => new ReadSucceededAction(response))
        .catch(error => Observable.of(new ReadFailedAction(error)));
    });

The testcase looks like this:


  it('readData$ effect should be initiated by READ action and then call the backend', () => {
    const initiator: ReadAction = new ReadAction('test1');
    actions = new ReplaySubject();
    actions.next(initiator);

    const expectedAction: ReadSucceededAction = new ReadSucceededAction(null);
    effects.readData$.subscribe((action: ReadSucceededAction) => {
      expect(action.type).toBe(expectedAction.type);
      expect(action.payload.message).toBe('a message from backend');
    });
    const req = httpMock.expectOne('/example/data/test1');
    expect(req.request.method).toEqual('GET');
    const responseData = {message: 'a message from backend'};
    req.flush(responseData);
    httpMock.verify();
  });

.. and the test is green.

Now when I duplicated the testcase, the duplicated test case is red with the follwoing error:
Error: Expected no open requests, found 1: GET /example/data/test1
In fact it receives data send by the first testcase and it fails due to "open requests".

Expected behavior:

The effect should only fire once per test case.

Minimal reproduction of the problem with instructions:

There is a demo repo ngrxEffectsFiredTwice containing the code, generated with angular cli. Just run "ng test" to see the failure,

Version of affected browser(s),operating system(s), npm, node and ngrx:

Chrome, Karma, Angular 5.0 like generated by angular cli, ngrc version is 4.1

Other information:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions