【问题标题】:How to read a file before runing a set of mocha test如何在运行一组 mocha 测试之前读取文件
【发布时间】:2019-04-08 02:55:01
【问题描述】:

我正在尝试读取包含一组 mocha 测试的所有输入的文件。在尝试读取这些文件时,我只是得到输入(地图)为空。

Scrapper_cheerio.js 以 async/await 格式编写,但是,不要相信这与未读取的文件有任何关系。

如果我之前理解正确,那么 before 块中的内容应该在任何测试发生之前发生。目前这是我尝试过的:

const assert = require('chai').assert;
const fs = require('fs');

const scrapper_cheerio = require('../scrapper/Scrapper_cheerio');
describe('test1', function(){
    var inputs = null;
    before( function(done){
        fs.readFile('./inputs.txt', 'utf8', 
            function(err, fileContents){
                if(err) throw err;
                const string_data = fileContents;
                const data = eval(string_data);
                inputs = new Map(data);
                done();
            });
    });

    describe('is_null_input()', function(){
        const is_null_input = inputs.get('is_null_input');
        const json_result = scrapper_cheerio.is_null_json(is_null_input);
        it('should return a json string', function(){
            assert.isObject(json_result, 'is json object');
       }); 
    });

});

我只是希望第一个测试是真的,因为它恰好是最容易测试的,但是我无法读取文件。我对摩卡测试很陌生,因此我可能会,并且可能确实有一些明显的错误。非常感谢任何和所有帮助。

编辑 1:

所以在快速被删除之前有人指出(为什么它真的很有帮助,谢谢。)我需要进行异步调用,我做到了。我现在可以读取文件,并且可以看到我正在读取它,但是我无法将值传递到底部

    describe('test1', function(done){
        var inputs = null;

        before('test1', function() {
            const string_data = fs.readFileSync('./inputs.txt', 'utf8');
            const data = eval(string_data);
            console.log('data uptop')
            inputs = new Map(data);
            console.log(inputs);
            done();
        });

        describe('is_null_input()', function(done){
            const is_null_input = inputs.get('is_null_input');
            var json_result = scrapper_cheerio.is_null_json(is_null_input);
            console.log('these are json results', json_result);
            it('should return a json string', function(){
                assert.isObject(json_result, 'is json object');
        }); 
        });

    });

所以我可以读取该文件,但它在描述中无法访问。

【问题讨论】:

  • 究竟是什么导致is_null_input() 失败?您收到任何错误消息吗?
  • 'Cannot read property 'get' of null' ,二维数组形式的字符串存储在该文件中。但是我相信该文件没有被读取。

标签: javascript node.js mocha.js


【解决方案1】:

这是一个简化的工作测试,应该可以帮助您快速入门:

inputs.txt

[[ 'is_null_input', true ]]

code.test.js

const fs = require('fs');
const expect = require('chai').expect;

describe('test1', function () {
  var inputs = null;

  before('test1', function (done) {
    fs.readFile('./inputs.txt', 'utf8', (err, data) => {
      if (err) throw err;
      inputs = new Map(eval(data));
      done();
    });
  });

  describe('is_null_input()', function () {
    it('should return a json string', function () {
      const is_null_input = inputs.get('is_null_input');
      expect(is_null_input).to.equal(true);  // Success!
      // ...
      // var json_result = scrapper_cheerio.is_null_json(is_null_input);
      // console.log('these are json results', json_result);
      // assert.isObject(json_result, 'is json object');
    });
  });

});

详情

Mocha run cycle首先运行所有describe回调,describe回调总是同步运行(你不能使用done,一个async函数,或者在describe中返回一个Promise回调)。

所以这一行:

const is_null_input = inputs.get('is_null_input');

...在before 之前运行,这意味着inputs 仍然是null

将代码移动到测试内部意味着它将在beforeinputs被定义之后运行

最好不要用readFileSync 阻塞JavaScript 事件循环,因此上面的示例将donereadFile 结合使用。

【讨论】:

    猜你喜欢
    • 2017-04-24
    • 1970-01-01
    • 2021-11-21
    • 2014-10-21
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 2016-08-07
    • 2019-11-03
    相关资源
    最近更新 更多