2017-11-23 5 views
1

私はを使って、process.arvをmochaのテストでstiして、cliアプリケーションを作成しようとしています。誤った引数( "imit")がprocess.argv(コマンドで定義されたもの)に渡されたときに、メッセージがconsole.loggedであることをテストしたい。mock-cliを設定する

私はドキュメントからその例を適用しようとしていますが、私はすべてを正しく設定しているとは思いません。私は「stdin: require('../mocks/fakeInputStream'), // Hook up a fake input stream」をコメントアウトするとき、それは渡さ

  1. 私は

、以下のように実行したとき、それは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); 
        }); 
    
  • 答えて

    0
    • 有効なパス../mocks/fakeInputStreamですか?

    • ../mocks/fakeInputStreamのオブジェクトはReadableStreamの有効なインスタンスですか?

    source codeはGitHubのでavalibleです。

    captureStdin(sourceStream, callback)機能の要件を満たしていることを確認してください。モジュールはその機能を使用してfakeInputStreamをキャプチャし、captureStreamにパイプします。

    関連する問題