Platform: Testing Error: this.actions$.ofType is not a function

Created on 24 Jul 2017  路  5Comments  路  Source: ngrx/platform

Hello,
I've been attempting follow the migration guide setting up specs for effects. However, I can't get the specs to work as outlined - I get TypeError: this.actions$.ofType is not a function.
Here's a snippet of the test:

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        AuthEffects,
        provideMockActions(() => actions),
        { provide: AuthService, useClass: StubAuthService },
        { provide: Router, useClass: StubRouter },
      ],
    });

    effects = TestBed.get(AuthEffects);
  });

  it('Should be created', () => {
    expect(effects).toBeTruthy();
  });

Here is full spec file: https://gist.github.com/d4hines/7b5fceb4a3eb53e050b9066800a02ccb
And Here is the full effects.ts file https://gist.github.com/d4hines/45749dcd69a8aef14a8b034aef1ba8a8

Why isn't provideMockActions() providing actual Actions? Am I doing something wrong is this an actual issue?

Most helpful comment

Seems that the problem still not solved

All 5 comments

Hello,

I have the same issue :s
However, look at https://github.com/ngrx/platform/blob/master/example-app/app/books/effects/collection.spec.ts. It works if you inject a custom action like the example.

Here is a the unit test of a router effect which works for me :

test-helper.ts

export class TestActions extends Actions {
  constructor() {
    super(empty());
  }

  set stream(source: Observable<any>) {
    this.source = source;
  }
}

export function getActions() {
  return new TestActions();
}

router-effect.ts

@Injectable()
export class RouterEffects {
  @Effect()
  myEffect$: Observable<Action> = this.actions$
    .ofType(ROUTER_NAVIGATION)
    ...
    .map(() => new MyAction());

  constructor(
    private store: Store<State>,
    private router: Router,
    private actions$: Actions
  ) {
  }
}

router-effect.spec.ts

beforeEach(() => {
      TestBed.configureTestingModule({
        imports: [
          StoreModule.forRoot(reducers, initialState),
          RouterTestingModule
        ],
        providers: [
          RouterEffects,
          {provide: Actions, useFactory: getActions}
        ]
      });

      routerEffects = TestBed.get(RouterEffects);
      actions$ = TestBed.get(Actions);
    }
  );`

it(
      'a router effect test',
      () => {
        actions$.stream = hot('--a', {a: {
          type: ROUTER_NAVIGATION,
          payload: myPayload
        }});
        const expectedResult = new MyAction();
        const expected = cold('--b', {b: expectedResult});

        expect(routerEffects.myEffect$).toBeObservable(expected);
      }
    );

This issue is fixed in master via https://github.com/ngrx/platform/pull/121

@brandonroberts - Do you know when #121 is going to get into a tagged release?

@adammartin1981 My quick and dirty way to fix this until it's tagged in a release was to just create my own file with the provideMockActions function and import from that file in my tests.
Copy the code from here into dirtyfix.ts

Then in your tests just import from that file:

// mytest.spec.ts
import { provideMockActions } from './dirtyfix.ts';

Seems that the problem still not solved

Was this page helpful?
0 / 5 - 0 ratings