与Chai相关的错误消息:“AssertionError:预期未定义为非常相等”

时间:2016-10-15 23:08:15

标签: javascript chai

我写了一个函数,当给出一个对象列表和一个 id,返回相同的列表,但相应的对象标记为活动 (所有其他对象不应处于活动状态)。

const list = [
     { id: 1, active: false },
     { id: 2, active: false },
     { id: 3, active: true  },
     { id: 4, active: false }
];

function markActive(list, value) {
  list.forEach((id) => {
    if (id.active = (id.id === value)) {
      return true;
    } else {
      return false;
    }
  });
}

markActive(list, 2);
console.log(list)

返回:

[ { id: 1, active: false },
  { id: 2, active: false },
  { id: 3, active: false },
  { id: 4, active: true } ]

它的工作就像一个魅力,除非我运行“npm run [filename]”我收到一条错误消息:

Running Tests for [filename].
------------
[ { id: 1, active: false },
  { id: 2, active: false },
  { id: 3, active: false },
  { id: 4, active: true } ]


  markActive
  1) Case 1 (Given Sample)
  2) Case 2 (String IDs)


  0 passing (16ms)
  2 failing

  1) markActive Case 1 (Given Sample):
     AssertionError: expected undefined to deeply equal [ { id: 1,        
     active: false },
     { id: 2, active: true },
     { id: 3, active: false },
     { id: 4, active: false } ]
     at Function.assert.deepEqual            
     (node_modules/chai/lib/chai/interface/assert.js:216:32)
      at Context.it (tests/test_02.js:23:12)

  2) markActive Case 2 (String IDs):
     AssertionError: expected undefined to deeply equal [ { id: '1',     
     active: false },
     { id: '2', active: true },
     { id: '3', active: false },
     { id: '4', active: false } ]
     at Function.assert.deepEqual   
     (node_modules/chai/lib/chai/interface/assert.js:216:32)
     at Context.it (tests/test_02.js:40:12)

知道我做错了什么吗?以下是设置测试的代码:

const chai    = require("chai");
const sinon   = require("sinon");
const assert  = chai.assert;

const markActive = require("../answers/02.js");

describe("markActive", () => {

  it("Case 1 (Given Sample)", () => {
  var list = [
         { id: 1, active: false },
         { id: 2, active: false },
         { id: 3, active: true  },
         { id: 4, active: false }
       ];
  var newList = markActive(list, 2);
  var targetList = [
         { id: 1, active: false },
         { id: 2, active: true  },
         { id: 3, active: false },
         { id: 4, active: false }
       ];
   assert.deepEqual(newList, targetList);
   });

  it("Case 2 (String IDs)", () => {
    var list = [
         { id: "1", active: false },
         { id: "2", active: false },
         { id: "3", active: true  },
         { id: "4", active: false }
       ];
    var newList = markActive(list, "2");
    var targetList = [
         { id: "1", active: false },
         { id: "2", active: true },
         { id: "3", active: false  },
         { id: "4", active: false }
       ];
    assert.deepEqual(newList, targetList);
  });

});

1 个答案:

答案 0 :(得分:1)

您的函数未返回任何内容,因此您尝试设置为结果的所有变量都将设置为undefined

要解决此问题,只需在函数末尾添加return语句即可。

function markActive(list, value) {
  list.forEach((id) => {
    if (id.active = (id.id === value)) {
      return true;
    } else {
      return false;
    }
  });

  return list; // return the updated list
}

注意:值得一提的是,由于数组已被引用,因此您可以就地修改这些值。这就是为什么即使您没有记录返回值,您在函数外部定义的数组仍然具有更新结果的原因。如果您在同一列表上多次运行markActive()函数,则可能会产生意外的副作用。如果您想要返回一个新列表,请查看在Javascript中复制和深度复制数组的方法。