NGXS:测试调度动作不适用于 ofActionDispatched
NGXS: Testing dispatched action is not working with ofActionDispatched
我正在尝试测试动作调度,但它在我的测试中不起作用...
Component 行为正常,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
块中。希望这有帮助,不确定我是否正确打开和关闭所有括号。
我正在尝试测试动作调度,但它在我的测试中不起作用... Component 行为正常,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
块中。希望这有帮助,不确定我是否正确打开和关闭所有括号。