即使两个数组完全相同,为什么我的测试仍然失败?

时间:2019-02-21 15:42:54

标签: javascript arrays node.js mocha chai

我正在discord bot内的迷你库中对我的函数进行单元测试,但我收到一条错误消息,说我的函数创建的数组与我键入的数组不相同,即使它们完全相同相同。该函数称为textToArray(),它采用文本文件的路径,并将其转换为数组(因此得名)。任何解决此问题的帮助将不胜感激。 functest.js(忽略其他测试):

/* eslint-env node, mocha */
const expect = require('chai').expect;
const funcs = require('../mainDefs');

describe('Check functions/variables from mainDefs.js', function () {
  it('Checks if textToArray works correctly', function () {
    const array = funcs.textToArray('./test/testarray.txt');
    const expectedArray = ['item1',
      'item2',
      'item3',
      'item 1 is cool',
      'item 2 is cool',
      'item 3 is cool',
      'You are awesome!',
      'If you are reading this you are probably contributing, if you are thanks!',
      'this is some very long text to see if textToArray breaks when I make it too long blah blah blah I like cake and rice (not together), I dont know why I am telling you that. Why are you even reading this?']
    console.log(array);
    console.log(expectedArray);
    expect(array).to.equal(expectedArray);
  });
  it('Checks if error is thrown when path is not a string for textToArray', function () {
    expect(() => funcs.textToArray({ path: 'Hello World' })).to.throw('Path supplied is not a string');
  });
  it('Checks if a site is safe', function (done) {
    funcs.checkLink('https://google.com', function (err) {
      if (err) throw err;
    });
    done();
  });
  it('Checks if a unsafe site is safe', function (done) {
    funcs.checkLink('http://malware.wicar.org/data/eicar.com', function (err) {
      if (err) throw err;
    });
    done();
  });
  it('Checks if a site is safe using a invalid service URI', function (done) {
    expect(funcs.checkLink.bind(funcs, 'http://malware.wicar.org/data/eicar.com', function (err) {
      if (err) throw err;
    }, 'blah')).to.throw('Invalid URI "blah"');
    done();
  });
  it('Checks if the Google Safe Browsing API returns an error when an invalid API key is used', function (done) {
    expect(() => funcs.checkLink('http://malware.wicar.org/data/eicar.com', function (err, data) {
      if (err) throw err;
      expect(data).to.equal(null);
    }, 'https://safebrowsing.googleapis.com/v4/threatMatches:find?key=boop'));
    done();
  });
  it('Checks the config var', function () {
    const config = funcs.config;
    expect(config).to.be.a('object');
  });
  it('Checks the giphy var', function () {
    const giphy = funcs.giphy;
    expect(giphy).to.be.a('object');
  });
});

testarray.txt:

item1
item2
item3
item 1 is cool
item 2 is cool
item 3 is cool
You are awesome!
If you are reading this you are probably contributing, if you are thanks!
this is some very long text to see if textToArray breaks when I make it too long blah blah blah I like cake and rice (not together), I dont know why I am telling you that. Why are you even reading this?

控制台(再次忽略其他测试,因为我在测试的不同元素上有多个文件):

Check functions/variables from mainDefs.js
[ 'item1',
  'item2',
  'item3',
  'item 1 is cool',
  'item 2 is cool',
  'item 3 is cool',
  'You are awesome!',
  'If you are reading this you are probably contributing, if you are thanks!',
  'this is some very long text to see if textToArray breaks when I make it too long blah blah blah I like cake and rice (not together), I dont know why I am telling you that. Why are you even reading this?' ]
[ 'item1',
  'item2',
  'item3',
  'item 1 is cool',
  'item 2 is cool',
  'item 3 is cool',
  'You are awesome!',
  'If you are reading this you are probably contributing, if you are thanks!',
  'this is some very long text to see if textToArray breaks when I make it too long blah blah blah I like cake and rice (not together), I dont know why I am telling you that. Why are you even reading this?' ]
    1) Checks if textToArray works correctly
    √ Checks if error is thrown when path is not a string for textToArray
    √ Checks if a site is safe
    √ Checks if a unsafe site is safe
    √ Checks if a site is safe using a invalid service URI
    √ Checks if the Google Safe Browsing API returns an error when an invalid API key is used
    √ Checks the config var
    √ Checks the giphy var

  Winston Define
    √ Define prolog

  Winston Prolog
error: [MAIN] Error Test
    √ Print an error to prolog
warn: [MAIN] Warn Test
    √ Print a warning to prolog

  Bot Startup
    √ Initalize
    √ Load main module
    √ Load moderation module
    √ Load fun module
    √ Load stats module
    √ Load giphy module
    √ Get error on load of a fake module
    √ Elevate Roles
    √ Cleaning up


  19 passing (96ms)
  1 failing

  1) Check functions/variables from mainDefs.js
       Checks if textToArray works correctly:

      AssertionError: expected [ Array(9) ] to equal [ Array(9) ]
      + expected - actual


      at Context.<anonymous> (test\functest.js:19:22)

0 个答案:

没有答案