我最近学习终极版,写单元测试与使用行动创造者和减速器玩笑Redux的单元测试 - 减速器和行动创造者
我写测试TDD过程的一部分。但我在努力:我可以在减速器测试中使用动作创建器吗?
import * as types from './../../constants/auth';
import * as actions from './../../actions/auth';
import reducer, {initialState} from './../auth';
我能做到这一点
it('should set isFetching to true',() => {
const expectedState = {
...initialState,
isFetching: true
}
expect(
reducer(initialState, actions.loginPending())
).toEqual(expectedState)
});
,而不是这个?
it('should set isFetching to true',() => {
const expectedState = {
...initialState,
isFetching: true
}
expect(
reducer(initialState, {type: types.LOGIN_PENDING})
).toEqual(expectedState)
});
我来到这无疑是因为官方文档使用硬编码的动作在减速测试:
expect(
reducer([], {
type: types.ADD_TODO,
text: 'Run the tests'
})
).toEqual([{
text: 'Run the tests',
completed: false,
id: 0
}])
我猜使用硬编码的行动是最好的做法是不是?
对此有相同的看法,主要是为了避免“维护成本”。但是,正如你声明的那样,它们使它成为声明式的,对新开发者来说很有帮助。由于测试不是单独运行,只需要对操作进行严格编码即可。谢谢 –