【问题标题】:seemingly equal objects are not equal看似相等的对象并不相等
【发布时间】:2018-05-21 22:51:19
【问题描述】:

我正在尝试测试 Redux 操作,但我在终端中收到此错误:
AssertionError: expected { type: 'LOG_IN', email: 'email' } to equal { type: 'LOG_IN' , email: 'email' } + 预期 - 实际

问题:这两个对象似乎是相等的,它们具有相同的属性和相同的值。我希望他们是平等的。但它们被报告为不相等。

myActions.js

import { LOG_IN,LOG_OUT } from './actionTypes';

export function logIn(email) {
 return {
   type: LOG_IN,
   email: email
 };
}

userReducer.test.js

import * as actions from '../actions/userActions';
import * as types from '../actions/actionTypes';
import user from '../reducers/userReducer';


describe('user login reducer test', () => {
   it('should update initialState to include email', () => {
    const email = 'email'
    const expectedAction = {
    type: LOG_IN,
    email: 'email'
 }

    const result = actions.logIn(email);
    expect(result).to.equal(expectedAction);
  })
 })

actiontypes.js

export const LOG_IN = 'LOG_IN';
export const LOG_OUT = 'LOG_OUT';

【问题讨论】:

  • 更改标题以更准确地反映问题。就像“Redux - 看似相等的对象并不相等”。

标签: javascript reactjs unit-testing redux jestjs


【解决方案1】:

看起来您正在使用Chai 进行断言,那么您应该使用eql 方法

expect(result).to.eql(expectedAction);

如果你想使用Jest,那么你可以使用toEqual方法

expect(result).toEqual(expectedAction);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-29
    • 2022-03-23
    • 1970-01-01
    • 2015-07-27
    • 2018-12-06
    • 2018-09-22
    • 1970-01-01
    • 2011-03-11
    相关资源
    最近更新 更多