与Chai的Redux测试问题

时间:2017-03-05 20:08:48

标签: testing redux mocha chai

我正在学习反应还原,我目前正在使用TDD实现一些减速器。我正在使用示例应用程序,它只是一个用于添加/删除/打开/关闭笔记的记事本。

我正在为添加新笔记进行一些测试,但第二个"它"失败了,引发我的错误如下:

1)AddNote reducer应返回两个注释:    AssertionError:期望{Object(id-123,id-456)}等于{Object(byId,ids,...)}

describe('AddNote reducer' , () => {
    it('should return a new note', () => {
        const state = getMockState.withNoNotes();
        const actualNextState = reducers.byId(
            state.byId, actions.addNote(
                'Hello World', 'id-123', 1));
        const expectedNextState = {
            'id-123': {
                id: 'id-123',
                content: 'Hello World',
                timestamp: 1
            }
        };
        expect(actualNextState).to.deep.equal(expectedNextState);
    });

    it('should return two notes', () => {
        const state = getMockState.withOneNote();
        const actualNextState = reducers.byId(state.byId, actions.addNote('Bye bye world!', 'id-456', 2));
        const expectedNextState = {
            ...state,
            'id-456': {
                id: 'id-456',
                content: 'Bye bye world!',
                timestamp: 2
            }
        };
        expect(actualNextState).to.deep.equal(expectedNextState);
    });
});

我还使用了帮助器,这里是

export const getMockState = {
    withNoNotes: () => ({
        byId: {},
        ids: [],
        openNoteId: null,
    }),
    withOneNote: () => ({
        byId: {
            'id-123': {
                id: 'id-123',
                content: 'Hello world',
                timestamp: 1,
            },
        },
        ids: ['id-123'],
        openNoteId: 'id-123',
    }),
};

我的想法是,如果我在第一种情况下添加一个音符,它将返回一个只有该音符的新状态,但在第二种情况下,它应该返回一个带有前一音符的状态,加上新音符。 我使用 Chai期望库使用mocha。

如果它有助于输出状态(它是控制台日志),我认为没问题,

{ 'id-123': { id: 'id-123', content: 'Hello World', timestamp: 1 } }
    √ should return a new note
{ 'id-123': { id: 'id-123', content: 'Hello world', timestamp: 1 },
  'id-456': { id: 'id-456', content: 'Bye bye world!', timestamp: 2 } }
    1) should return two notes

这里是我的减速机

import { merge } from 'ramda';

export const byId = (state={}, {type, payload}) => {
    switch(type) {
        case 'app/addNote':
            console.log(merge(state, { [payload.id]: payload})) ;
            return merge(state, { [payload.id]: payload});
        default:
            return state;
    }
};

有什么想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:1)

您的expectedState不正确。您正在比较"下一个状态" byId切片,但您的expectedNextState扩展了整个州。它应该是:

const expectedNextState = {
    ...state.byId,
    'id-456': {
        id: 'id-456',
        content: 'Bye bye world!',
        timestamp: 2
    }
};