在Angular ngxs单元测试中,如果你发现dispatch方法不起作用,可能是由于一些原因导致的。以下是一些可能的解决方法:
import { StoreTestingModule } from '@ngxs/store/testing';
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [StoreTestingModule]
}).compileComponents();
}));
import { MockStore, NgxsTestingModule } from '@ngxs/testing';
let store: MockStore;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [NgxsTestingModule]
}).compileComponents();
store = TestBed.inject(MockStore);
}));
it('should dispatch action', () => {
store.dispatch(new YourAction());
expect(store.dispatch).toHaveBeenCalledWith(new YourAction());
});
import { YourAction } from './your-action';
import { YourSelector } from './your-selector';
// ...
it('should dispatch action', () => {
store.dispatch(new YourAction());
expect(store.dispatch).toHaveBeenCalledWith(new YourAction());
});
it('should select state', () => {
const state = store.selectSnapshot(YourSelector.getState);
expect(state).toEqual(expectedState);
});
通过以上方法,你应该能够解决Angular ngxs单元测试中dispatch方法不起作用的问题。