所以我认为我创建了一个简单的测试来确认我的redux减少器的mocha / chai测试设置,但是这个初始测试由于我无法辨别的原因而失败。一切都是根据babel docs设置的。没有与es6相关的错误。
文件结构
client
|-reducers
| |-auth_reducer.js
|
test
|-reducers
| |-auth.test.js
测试脚本
mocha --compilers js:babel-register --require babel-polyfill --recursive
Auth Reducer
const AUTH_USER = 'AUTH_USER';
const INITIAL_STATE = { error: '', message: '', content: '', authenticated: false, user: null };
export default function (state = INITIAL_STATE, action) {
switch (action.type) {
case AUTH_USER:
return { ...state, error: '', message: '', authenticated: true };
}
return state;
}
验证测试
import { expect } from 'chai';
import authReducer from '../../client/reducers/auth_reducer';
describe('auth_reducer', () => {
it('handles AUTH_USER', () => {
const initialState = {
error: '',
message: '',
content: '',
authenticated: false,
user: null
};
const action = {
type: 'AUTH_USER',
};
const nextState = authReducer(initialState, action);
expect(nextState).to.equal({
error: '',
message: '',
content: '',
authenticated: true,
user: null
});
});
});
错误
auth_reducer
1) handles AUTH_USER
0 passing (38ms)
1 failing
1) auth_reducer handles AUTH_USER:
AssertionError: expected { Object (error, message, ...) } to equal { Object (error, message, ...) }
+ expected - actual
at Context.<anonymous> (test/reducers/auth.test.js:20:26)
npm ERR! Darwin 15.6.0
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "test"
npm ERR! node v6.7.0
npm ERR! npm v3.10.9
npm ERR! code ELIFECYCLE
npm ERR! yars@0.0.1 test: `mocha --compilers js:babel-register --require babel-polyfill --recursive`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the yars@0.0.1 test script 'mocha --compilers js:babel-register --require babel-polyfill --recursive'.
答案 0 :(得分:2)
使用deep.equal
代替equal
:
expect(nextState).to.deep.equal({
error: '',
message: '',
content: '',
authenticated: true,
user: null
});
当您对对象进行浅层比较时,对象&#39;比较参考。使用深度比较时,会比较属性和值。
一个简单的例子:
const a = { val1: 1, val2: 2 };
const b = { val1: 1, val2: 2 };
console.log('equality ', a === b);
console.log('deep equality ', a.val1 === b.val1 && a.val2 === b.val2);
&#13;