在此操作中,我们初始化new Student()
但仅在传入stuff
的情况下。在测试中,不应传递stuff
,因为该方法仅使用reducer初始化
在测试中,由于new Student()
初始化TypeError: undefined is not an object (evaluating 'class_1.ClassActions.CLASS_INIT')
,它失败了。如果删除了new Student()
部分,则测试通过时没有任何问题。
当记录stuff
时,它显示为null
并且条件块中的任何日志都没有实际注销(因为它没有正式到达该块)。
为什么这个测试会影响代码的这一部分?是否有一种我缺少的解决方法?
动作:
import { Injectable } from '@angular/core';
import { NgRedux } from 'ng2-redux';
import { IAppState } from '../../store';
import { Student } from './../../store/class';
@Injectable()
export class ClassActions {
public static CLASS_INIT = 'CLASS_INIT';
constructor(
private ngRedux: NgRedux<IAppState>
) { }
public classInit(stuff) {
this.ngRedux.dispatch({
type: ClassActions.CLASS_INIT
});
if (stuff) {
let student: Student = new Student();
// other logic
};
}
}
减速
import { ClassActions } from '../../actions/class';
export class Student {
public data: any = {};
public apps: any = [];
}
export interface IClassState {
students: { [key: string]: Student };
}
export const INITIAL_STATE: IClassState = {
students: {}
};
export function classStateReducer(state: IClassState = INITIAL_STATE, action: any) {
switch (action.type) {
case ClassActions.CLASS_INIT:
return Object.assign({}, state, {});
default:
return state;
}
};
Reducer.spec:
import { classStateReducer, INITIAL_STATE } from './class.reducer';
import { ClassActions } from './../../actions/class';
describe('Class Reducer', () => {
it('should handle ClassActions.CLASS_INIT', () => {
expect(
classStateReducer(INITIAL_STATE, {
type: ClassActions.CLASS_INIT
})
).toEqual(INITIAL_STATE);
});
});