本文介绍了如何对该效果进行单元测试(使用{dispatch:false})?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ngrx和单元测试初学者在这里.我有以下效果:
ngrx and unit testing beginner here. I have the following effect:
@Injectable()
export class NotificationEffects {
@Effect({dispatch: false})
notificationShow$ = this.actions$
.ofType(notificationAction.NOTIFICATION_SHOW)
.do((action: notificationAction.NotificationShowAction) => {
this.notificationService.info(action.payload.config);
});
constructor(private actions$: Actions, private notificationService: NotificationService) {}
}
具体来说,我想测试是否已调用notificationService方法信息.我该怎么办?
Specifically, I would like to test that the notificationService method info has been called. How would I do that?
我已遵循以下示例,但未找到解决方案:
I have followed these examples but not found a solution:
https://netbasal.com/unit-test- your-ngrx-effects-in-angular-1bf2142dd459 https://medium.com/@adrianfaciu/testing-ngrx-effects-3682cb5d760e一个> https://github.com/ngrx/effects/blob/master/docs/testing.md
推荐答案
所以就这么简单:
describe('notificationShow$', () => {
let effects: NotificationEffects;
let service: any;
let actions$: Observable<Action>;
const payload = {test: 123};
beforeEach( () => {
TestBed.configureTestingModule( {
providers: [
NotificationEffects,
provideMockActions( () => actions$ ),
{
provide: NotificationService,
useValue: jasmine.createSpyObj('NotificationService', ['info'])
}
]
} );
effects = TestBed.get(NotificationEffects);
service = TestBed.get(NotificationService);
});
it('should call a notification service method info with a payload', () => {
actions$ = cold('a', { a: new notificationAction.NotificationShowAction(payload) });
effects.notificationShow$.subscribe(() => {
expect(service.info).toHaveBeenCalledWith(payload);
});
});
});
这篇关于如何对该效果进行单元测试(使用{dispatch:false})?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!