Angular 如何使用MockStore在角度服务中测试NgRx调度操作
我正在寻找一种在服务中测试dispach函数的方法。我有这样一个动作:Angular 如何使用MockStore在角度服务中测试NgRx调度操作,angular,testing,karma-jasmine,ngrx,ngrx-store,Angular,Testing,Karma Jasmine,Ngrx,Ngrx Store,我正在寻找一种在服务中测试dispach函数的方法。我有这样一个动作: export const start = createAction( '[App] Started' ); export const appReducer = createReducer( initialState, on(AppActions.start, (state: State) => ({ ...state, isStarted: true })) 这样的减速器: expo
export const start = createAction(
'[App] Started'
);
export const appReducer = createReducer(
initialState,
on(AppActions.start, (state: State) => ({
...state,
isStarted: true
}))
这样的减速器:
export const start = createAction(
'[App] Started'
);
export const appReducer = createReducer(
initialState,
on(AppActions.start, (state: State) => ({
...state,
isStarted: true
}))
在要在存储中设置的服务中,已启动
为true:
start(): void {
this.store.dispatch(start());
}
我发现了很多例子,但都有一个组件。这是我的规格:
describe('StartService', () => {
let service: StartService;
let store: MockStore;
const initialState: State = {
isLoading: false
};
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
StartService,
provideMockStore({ initialState }),
],
});
service = TestBed.inject(StartService);
store = TestBed.inject(MockStore);
});
it('it should test start', () => {
const dispatchSpy = spyOn(store, 'dispatch').and.callThrough(); // spy on the store
service.start(); // <----- My function to test
expect(store.dispatch).toHaveBeenCalled();
});
}
description('StartService',()=>{
let服务:StartService;
让商店:MockStore;
常量initialState:状态={
孤岛加载:false
};
在每个之前(()=>{
TestBed.configureTestingModule({
供应商:[
StartService,
provideMockStore({initialState}),
],
});
service=TestBed.inject(StartService);
store=TestBed.inject(MockStore);
});
它('它应该测试开始',()=>{
const dispatchSpy=spyOn(存储区,'dispatch')。和.callThrough();//监视存储区
service.start()//