异步读取摩卡测试中的文件?
Read files in mocha tests asynchronously?
我没有得到输入和输出变量的值。我也尝试在下面的代码中使用此关键字。
it("Final Decoding Tests", () => {
let input = "";
let output = "";
fs.readFile("./test/test_data/booksEncoded.txt", { encoding: "utf-8" }, (err, data) => {
if (!err) {
this.input = data;
} else {
console.log(err);
}
});
fs.readFile("./test/test_data/books.xml", { encoding: "utf-8" }, (err, data) => {
if (!err) {
this.output = data;
} else {
console.log(err);
}
});
console.log(input); // NO OUTPUT
console.log(this.output); //PRINTS undefined
});
我想我必须使用 done
回调异步读取文件。
我的问题是:
为什么我在 fs.readFile 方法之外没有获得任何输入和输出值?有没有办法使用 done 关键字异步读取它?
您可以使用 util.promisify(original)
方法获取 fs.readFile
的承诺版本。
例如
index.spec.js
:
const util = require("util");
const path = require("path");
const fs = require("fs");
const { expect } = require("chai");
const readFile = util.promisify(fs.readFile);
describe("57841192", () => {
it("Final Decoding Tests", async () => {
const basepath = path.resolve(__dirname, "./test/test_data");
const options = { encoding: "utf-8" };
const readInput = readFile(path.join(basepath, "booksEncoded.txt"), options);
const readOutput = readFile(path.join(basepath, "books.xml"), options);
const [input, output] = await Promise.all([readInput, readOutput]);
expect(input).to.be.equal("haha");
expect(output).to.be.equal("<item>unit test</item>");
console.log(input);
console.log(output);
});
});
单元测试结果:
57841192
haha
<item>unit test</item>
✓ Final Decoding Tests
1 passing (9ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.spec.js | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/Whosebug/57841192
我没有得到输入和输出变量的值。我也尝试在下面的代码中使用此关键字。
it("Final Decoding Tests", () => {
let input = "";
let output = "";
fs.readFile("./test/test_data/booksEncoded.txt", { encoding: "utf-8" }, (err, data) => {
if (!err) {
this.input = data;
} else {
console.log(err);
}
});
fs.readFile("./test/test_data/books.xml", { encoding: "utf-8" }, (err, data) => {
if (!err) {
this.output = data;
} else {
console.log(err);
}
});
console.log(input); // NO OUTPUT
console.log(this.output); //PRINTS undefined
});
我想我必须使用 done
回调异步读取文件。
我的问题是:
为什么我在 fs.readFile 方法之外没有获得任何输入和输出值?有没有办法使用 done 关键字异步读取它?
您可以使用 util.promisify(original)
方法获取 fs.readFile
的承诺版本。
例如
index.spec.js
:
const util = require("util");
const path = require("path");
const fs = require("fs");
const { expect } = require("chai");
const readFile = util.promisify(fs.readFile);
describe("57841192", () => {
it("Final Decoding Tests", async () => {
const basepath = path.resolve(__dirname, "./test/test_data");
const options = { encoding: "utf-8" };
const readInput = readFile(path.join(basepath, "booksEncoded.txt"), options);
const readOutput = readFile(path.join(basepath, "books.xml"), options);
const [input, output] = await Promise.all([readInput, readOutput]);
expect(input).to.be.equal("haha");
expect(output).to.be.equal("<item>unit test</item>");
console.log(input);
console.log(output);
});
});
单元测试结果:
57841192
haha
<item>unit test</item>
✓ Final Decoding Tests
1 passing (9ms)
---------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
---------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.spec.js | 100 | 100 | 100 | 100 | |
---------------|----------|----------|----------|----------|-------------------|
源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/Whosebug/57841192