【发布时间】: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