configuration模拟cli
我试图使用模拟cli来存储cli应用程序的摩卡testingprocess.arv。 我想testing一个消息是console.logged当一个不正确的参数(“imit”)被传递给process.argv(由命令定义)。
我正在尝试修改文档中的示例,但是我不认为我已经正确设置了所有内容。
- 它通过时,我注释掉“
stdin: require('../mocks/fakeInputStream'), // Hook up a fake input stream
”虽然我知道它不能正常工作 - 它与
TypeError: sourceStream.on is not a function
失败TypeError: sourceStream.on is not a function
运行时TypeError: sourceStream.on is not a function
,如下所述
有人可以看到我失踪?
/index.js
var commands = ['init']; function getGitHeadArgs() { return process.argv.slice(2, process.argv.length); } if (getGitHeadArgs().length) { if (!commands.includes(getGitHeadArgs()[0])) { console.log("Silly Githead! That's not a githead command"); } eval(getGitHeadArgs()[0])(); } else { console.log("You didn't tell githead to do anything!"); }
/testIndex.js
var assert = require('assert'); var index = require('../index.js'); var mockCli = require("mock-cli"); describe("incorrect argument", function() { it("imit throws an error if an invalid command is raised", function() { var argv = ['node', '../index.js', 'imit']; // Fake argv var stdio = { stdin: require('../mocks/fakeInputStream'), // Hook up a fake input stream stdout: process.stdout, // Display the captured output in the main console stderr: process.stderr // Display the captured error output in the main console }; var kill = mockCli(argv, stdio, function onProcessComplete(error, result) { var exitCode = result.code; // Process exit code var stdout = result.stdout; // UTF-8 string contents of process.stdout var stderr = result.stderr; // UTF-8 string contents of process.stderr assert.equal(exitCode, 0); assert.equal(stdout, "Silly Githead! That's not a githead command\n"); assert.equal(stderr, ''); }); // Execute the CLI task require('../index.js'); // Kill the task if still running after one second setTimeout(kill, 1000); });
-
../mocks/fakeInputStream
是一个有效的path吗? -
../mocks/fakeInputStream
的对象是../mocks/fakeInputStream
的有效实例吗?
源代码在GitHub上可用。
确保满足captureStdin(sourceStream, callback)
函数的要求。 该模块使用该函数来捕获您的fakeInputStream
并将其fakeInputStream
到captureStream
。
- 我怎样才能使用JEST间谍来testing一个NodeJs模块/应用程序
- 如何在Node.js中模拟依赖testing?
- 使用mockgoose为mongoose(mongodb库为node.js)嘲笑填充方法是null
- 节点jsunit testing:嘲笑需要依赖
- 存根/模拟process.platform sinon
- testingNode.js,模拟并testing一个需要的模块?
- 使用proxyquire进行模拟的策略需要跨越多个testing文件(NodeJS)
- 如何模拟来自http.request(nodejs,jasmine,sinon)的响应
- 使用Node的serialportcallback模拟/unit testing