使用 mocha/chai 进行基本的 React/redux 测试 [英] Basic React/redux testing with mocha/chai

查看:53
本文介绍了使用 mocha/chai 进行基本的 React/redux 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 react/redux 世界的新手,我使用 this 创建了 redux todo 应用程序代码,现在我想为它编写测试,我创建了以下规范:

I'm new to react/redux world, I created the redux todo app using this code, Now I wanted to write tests for it, I create following spec:

import {expect} from 'chai'
import todoApp from '../src/reducers'
import {setVisibilityFilter} from '../src/actions'

describe('reducer', ()=> {
    it('Set correctly visibility filter', ()=>{
        const setVisibilityFilterAction = setVisibilityFilter('SHOW_ALL')
        const initialState={};
        const nextState= todoApp(initialState, setVisibilityFilterAction)
        const expectedState ={
                todos:[],
                visibilityFilter:'SHOW_ALL' 
            }
        expect(nextState).to.equal(expectedState);
    })
})  

但是断言失败了,错误是:

However the assertion went fail and the error is:

AssertionError: expected { Object (todos, visibilityFilter) } to equal { Object (todos, visibilityFilter) }
      + expected - actual

尝试打印对象并且它们(expectedStatenextState)都具有相同的数据:

Tried to print the object and they(expectedState and nextState) both have the same data:

{
   todos:[],
   visibilityFilter: "SHOW_ALL"
}

为什么会失败?

推荐答案

equal()<来自 Chai 的 expect 接口的/code> 是一个严格的相等性检查.听起来您想要 eql().deep.equal().

equal() from Chai's expect interface is a strict equality check. It sounds like you want eql() or .deep.equal().

// Equivalent to nextState === expectedState
expect(nextState).to.equal(expectedState);

// Instead:
expect(nextState).to.eql(expectedState);

// Or:
expect(nextState).to.deep.equal(expectedState);

这篇关于使用 mocha/chai 进行基本的 React/redux 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆