gpt4 book ai didi

angular - NGXS:测试调度 Action 不适用于 ofActionDispatched

转载 作者:行者123 更新时间:2023-12-05 06:29:27 25 4
gpt4 key购买 nike

我正在尝试测试 Action 调度,但它在我的测试中不起作用...组件表现良好, Action 被调度,但 actions$.pipe 没有捕捉到 Action ,因此无法在测试中检查。

代码:

describe('ResponsiveService', () => {
let store, actions$;
const observableMediaMock = {
subscribe : (callback: (change: MediaChange) => void) => {
this.callback = callback
},
triggerMediaChange: (change: MediaChange) => this.callback(change)
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
FlexLayoutModule,
NgxsModule.forRoot([MediaState])
],
providers: [ResponsiveService, { provide: ObservableMedia, useValue: observableMediaMock }]
});
store = TestBed.get(Store);
actions$ = TestBed.get(Actions);
});

it('should be created', inject([ResponsiveService], (service: ResponsiveService) => {
expect(service).toBeTruthy();
}));

it("should send get request", function(done) {
inject([ResponsiveService], (service: ResponsiveService) => {
const mediaQueryValue = 'mediaQuery',
mqAliasValue = 'mqAlias',
suffixValue = 'suffix';

actions$.pipe(ofActionDispatched([MediaChanged]))
.subscribe(({payload}) => {
expect(payload).toBeTruthy();
expect(payload.mediaQuery).toEqual(mediaQueryValue);
done();
});
service.init();

observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
})(); // function returned by 'inject' has to be invoked
});
});

谢谢!

最佳答案

将您的测试更新为以下内容:

it("should send get request", async( () => {
inject([ResponsiveService], (service: ResponsiveService) => {
const mediaQueryValue = 'mediaQuery',
mqAliasValue = 'mqAlias',
suffixValue = 'suffix';

actions$.pipe(ofActionDispatched(MediaChanged))
.subscribe(({payload}) => {
expect(payload).toBeTruthy();
expect(payload.mediaQuery).toEqual(mediaQueryValue);
});
service.init();

observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
})(); // function returned by 'inject' has to be invoked
}));

ofActionDispatched 不将列表作为输入,但评分者采用项目列表,例如:ofActionDispatched(MediaChanged, SomeOtherAction)

您还应该能够删除 done 函数并将测试包装在普通的 async block 中。希望这会有所帮助,但不确定我是否正确打开和关闭了所有括号。

关于angular - NGXS:测试调度 Action 不适用于 ofActionDispatched,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53502991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com