I would like to override a service provider when testing Ngrx Effects on a per-test basis to cover a success and failure response.
What I've tried so far is providing my service in the TestBed as such:
describe('Account Effects', () => {
let actions: ReplaySubject<any>;
let effects: AccountEffects;
let store: Store<IAccountsState>;
const email = 'someone@gmail.com';
const password = 'abc123!';
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
],
providers: [
AccountEffects,
provideMockActions(() => actions),
provideMockStore<IAccountsState>({ initialState }),
{
provide: AccountsService,
useValue: { _signIn: () => of(null) }
}
]
});
store = TestBed.get(Store);
});
beforeEach(async () => {
effects = TestBed.get(AccountEffects);
});
});
Then in each test, override the provider:
describe('signIn$ success', () => {
beforeEach(() => {
TestBed.overrideProvider(AccountsService, {
useValue: {
_signIn: () => of({ authenticated: true })
}
});
});
it('should dispatch signInSuccess when a successful response is received', () => {
actions = new ReplaySubject(1);
actions.next(AccountActions.signIn({ email, password }));
effects.signIn$.subscribe(result => {
expect(result).toEqual(AccountActions.signInSuccess({ email }));
});
});
});
The problem I'm having is that in the test, accountService._signIn is not a function:
An error was thrown in afterAll TypeError:
this.accountsService._signIn is not a function
I need to be able to override the value returned in each test case.
I have been following:
https://indepth.dev/testing-and-faking-angular-dependencies/
What am I doing wrong?