I want to test an action in NGXS but failing because of a service I need to mock. Googling a found out there are multiple ways to mock a service.
Test
describe('UserState', () => {
let store: Store;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
NgxsModule.forRoot([UserState]),
HttpClientModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFirestoreModule,
AngularFireAuthModule
],
providers: [{provide: DatabaseService, useValue: {updateUserField: of(1)}}]
});
store = TestBed.get(Store);
});
it('should update user settings', () => {
store.dispatch(new UpdateUserSettingsAction({alias: 'Naruto'} as Settings));
const userModel: UserModel = store.selectSnapshot(state => state.user);
expect(userModel.settings).toBeDefined();
});
});
Action
@Action(UpdateUserSettingsAction)
updateUserSettings(ctx: StateContext<UserModel>, action: UpdateUserSettingsAction) {
const user = ctx.getState();
return this.databaseService.updateUserField('settings', action.settings)
.pipe(
first(),
tap(() => {
ctx.setState({
...user,
settings: action.settings
});
})
);
}
Error
this.databaseService.updateUserField is not a function
Do you see my mistake?
The problem is that updateUserField is a function in your real service, but in your mock it is just a property. The following change should make it work:
All I have done is added the
() =>
, so that it becomes a function that returns yourof(1)