首页 > 解决方案 > 即使预期和结果相同,使用 Mocha 测试 String.split() 也会失败

问题描述

我目前正在进入 TDD,我想为一个简单的“String.split()”编写一个测试。

该函数应返回包含在字符串中的单词数组。

export function splitSentence(inputSentence) {
    const result = inputSentence.split(' ');
    return result;
}

mocha用作测试框架和chai断言。

这是测试。

describe('String Split', () => {
  it('should split a string into an array of words', () => {
    const inputString = 'This is some text';
    const expectedResult = ['This', 'is', 'some', 'text'];
    expect(splitSentence(inputString)).to.equal(expectedResult);
  });
});

在浏览器控制台中测试所有内容时,它运行良好,因此我确信它会通过测试。但是它失败并显示以下错误消息。

  AssertionError: expected [ 'This', 'is', 'some', 'text' ] to equal [ 'This', 'is', 'some', 'text' ]`

查看错误消息后,它们似乎都是平等的,所以我目前想知道这里出了什么问题。

感谢您的帮助。

标签: javascriptunit-testingmocha.jschai

解决方案


推荐阅读