Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
d4hines opened this issue Jul 24, 2017 · 5 comments
Closed

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

d4hines opened this issue Jul 24, 2017 · 5 comments

Comments

@d4hines
Copy link

d4hines commented Jul 24, 2017

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?

@ThomasSalvetat
Copy link

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);
      }
    );

@brandonroberts
Copy link
Member

This issue is fixed in master via #121

@adammartin1981
Copy link

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

@jckeen
Copy link

jckeen commented Jul 28, 2017

@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';

@AlexNasonov
Copy link

Seems that the problem still not solved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants