使用来自 mocha 测试套件的参数调用 index.js
Call index.js with arguments from mocha test suite
我有一个需要一些参数的 index.js
。
// parameters
var outFile = process.argv[2] || (() => {throw "missing argument outFile";})();
var templateName = process.argv[3] || (() => {throw "missing argument templateName";})();
现在我想测试使用参数调用 index.js,不是测试函数本身,而是测试参数验证。
有办法像这样编写 mocha 套件
var assert = require('assert');
describe('Wenn calling index.js', function() {
describe('with arguments arg1 arg2', function() {
it('should should fail because of "missing argument outFile"', function() {
...
});
});
});
process
是 nodejs 应用程序中的全局变量,因此您应该能够在测试中设置所需的参数。您可以使用 afterEach
挂钩重置 process.argv
。
var assert = require('assert');
describe('Wenn calling index.js', function() {
describe('with arguments arg1 arg2', function() {
afterEach(function(){
process.argv = process.argv.slice(0,2);
});
it('should should fail because of "missing argument outFile"', function() {
process.argv[3] = "param templateName";
require("path/to/index.js");
});
});
});
我有一个需要一些参数的 index.js
。
// parameters
var outFile = process.argv[2] || (() => {throw "missing argument outFile";})();
var templateName = process.argv[3] || (() => {throw "missing argument templateName";})();
现在我想测试使用参数调用 index.js,不是测试函数本身,而是测试参数验证。
有办法像这样编写 mocha 套件
var assert = require('assert');
describe('Wenn calling index.js', function() {
describe('with arguments arg1 arg2', function() {
it('should should fail because of "missing argument outFile"', function() {
...
});
});
});
process
是 nodejs 应用程序中的全局变量,因此您应该能够在测试中设置所需的参数。您可以使用 afterEach
挂钩重置 process.argv
。
var assert = require('assert');
describe('Wenn calling index.js', function() {
describe('with arguments arg1 arg2', function() {
afterEach(function(){
process.argv = process.argv.slice(0,2);
});
it('should should fail because of "missing argument outFile"', function() {
process.argv[3] = "param templateName";
require("path/to/index.js");
});
});
});