2017-01-03 3 views
0

私は簡単なテストを作成して、私の還元レデューサーのために設定したモカ/チャイテストを確認したと思ったが、この最初のテストは分からない理由で失敗している。すべてはbabel docsに従って設定されています。 es6関連のエラーはありません。還元レデューサーのモカ試験に失敗したのはなぜですか?

ファイル構造

client 
|-reducers 
| |-auth_reducer.js 
| 
test 
|-reducers 
| |-auth.test.js 

テストスクリプト
mocha --compilers js:babel-register --require babel-polyfill --recursive

認証リデューサー

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 
    }); 
    }); 
}); 
代わりに equal

エラー

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! [email protected] test: `mocha --compilers js:babel-register --require babel-polyfill --recursive` 
npm ERR! Exit status 1 
npm ERR! 
npm ERR! Failed at the [email protected] test script 'mocha --compilers js:babel-register --require babel-polyfill --recursive'. 

答えて

2

使用deep.equal:あなたが浅くオブジェクトを比較しているとき

expect(nextState).to.deep.equal({ 
    error: '', 
    message: '', 
    content: '', 
    authenticated: true, 
    user: null 
}); 

、オブジェクトの参照が比較されます。ディープ比較を使用すると、プロパティと値が比較されます。

簡単な例:

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);

+0

聖なる神はあなたに感謝します。私はなぜgoogleに行きますか? – Kwhitejr

+0

私は単純な例で "なぜ"を追加しました。 –

関連する問題