【问题标题】:EventEmitter unit test using mocha and sinon doesn't work使用 mocha 和 sinon 的 EventEmitter 单元测试不起作用
【发布时间】:2021-04-26 22:54:52
【问题描述】:

我的代码“expect(spy.call).to.be.true”部分应该是真的。但是测试结果显示是这样的。

  AssertionError: expected false to be true
  + expected - actual

  -false
  +true

我尝试用 mocha 对 EventEmitter 进行单元测试。很快,我意识到我需要检查监听器函数是否被调用。所以我导入了 sinon 模块。但我所期待的一切都没有发生在我身上。 我的代码如下。请让我知道我的代码有什么问题。非常感谢。

const findPattern = require('./findPatterns');
const expect = require('chai').expect;
const sinon = require('sinon');
const FakeTimers = require('@sinonjs/fake-timers');

describe('findPattern', function() {

  const sampleFiles = ['./sample/sample1.txt', './sample/sample2.txt', './sample/sample3.txt'];
  const regEx = /[0-9]+/g;

  it('findPattern', function() {
    const clock = FakeTimers.createClock();

    const o = {
      f: (file) => console.log('Read from ' + file)
    };

    const spy = sinon.spy(o, "f");

    findPattern(sampleFiles, regEx)
      .on('error', (err) => console.log('error: ' + err))
      .on('read', spy)
      .on('found', (file, match) => console.log('Matched ' + match + ' in file' + file));

    clock.setTimeout(function() {
      expect(spy.called).to.be.true;
    }, 1000);

    clock.tick(1000);
  });
});

const EventEmitter = require('events');
const fs = require('fs');

function findPattern(files, regex) {
  const emitter = new EventEmitter();

  files.forEach(function(file) {
    fs.readFile(file, "utf-8", (err, content) => {
      if (err) {
        return emitter.emit('error', err);
      }

      emitter.emit('read', file);

      const matches = content.match(regex);
      if (matches) {
        matches.forEach(i => {
          emitter.emit('found', file, i);
        });
      }
    }); 
  });

  return emitter;
}

module.exports = findPattern;

【问题讨论】:

    标签: javascript mocha.js sinon eventemitter


    【解决方案1】:

    嗯,我想我找到了一种解决方案。我猜这个问题是关于异步问题的。在完成读取文件之前,似乎调用了“期望函数”。 所以我改变了我的代码并得到了我期望的结果。顺便说一下,计时器功能似乎无法正常工作。请检查我更改的代码,如果我做错了什么,请提供一些建议。

    const findPattern = require('./findPatterns');
    const expect = require('chai').expect;
    const sinon = require('sinon');
    
    describe('findPattern', function() {
      const sampleFiles = ['./sample/sample1.txt', './sample/sample2.txt', './sample/sample3.txt'];
      const regEx = /[0-9]+/g;
      
      const readFun = (file) => console.log('Read from ' + file);
      const o = {
        f: readFun
      };
    
      const spy = sinon.spy(o, "f");
    
      beforeEach(function(done) {
        findPattern(sampleFiles, regEx, done)
          .on('error', (err) => console.log('error: ' + err))
          .on('read', spy)
          .on('found', (file, match) => console.log('Matched ' + match + ' in file' + file));
      });
    
      it('findPattern', function() {
        expect(spy.called).to.be.true;
      });
    });
    
    const EventEmitter = require('events');
    const fs = require('fs');
    
    function readFile(file, regex, emitter, callback) {
      fs.readFile(file, "utf-8", (err, content) => {
        if (err) {
          return emitter.emit('error', err);
        }
        
        emitter.emit('read', file);
        
        const matches = content.match(regex);
        if (matches) {
          matches.forEach(i => {
            emitter.emit('found', file, i);
          });
        }
    
        callback();
      }); 
    }
    
    function findPattern(files, regex, done) {
      const emitter = new EventEmitter();
    
      let completed = 0;
    
      function finish() {
        if (++completed == files.length) {
          return done();
        } 
      }
    
      files.forEach(function(file) {
        readFile(file, regex, emitter, finish);
      });
    
      return emitter;
    }
    
    module.exports = findPattern;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-03
      • 2021-09-27
      • 2023-04-09
      • 2023-04-11
      • 1970-01-01
      • 2019-02-14
      • 1970-01-01
      • 2018-11-05
      相关资源
      最近更新 更多