2017-11-28 13 views
0

私はTDDを初めて使い、ユニットテストを書くのにIntern v4を使用しています。私はドキュメントを通過しましたが、私はまだテストを書くのに問題があります。誰かが私を正しい方向に向けることができれば感謝します。次のようにインターンを使ってプレーンジャバスクリプトをテストするには

私のjavascriptのコードは次のとおりです。

var app = (function(){ 
 
    let name = 'Mufasa'; 
 
    let fullname = ''; 
 
    return { 
 
     print: function(surname) { 
 
      fullname = `hello ${name} ${surname}`; 
 
      console.log(fullname); 
 
      return fullname; 
 
     } 
 
    } 
 
})(); 
 

 
app.print('Vader');

私はNPM経由で私のプロジェクトでのインターンが含まれています。次のように

私のユニットテストファイルtest1.jsは次のとおりです。

const { suite, test } = intern.getInterface('tdd'); 
 
const { assert } = intern.getPlugin('chai'); 
 

 

 

 
// how do i write my first test case here to check the output when i pass/do not pass the surname parameter

答えて

0

あなたは非モジュラJSを使用しているので、あなたのアプリケーションがグローバル名前空間にロードされます(すなわち、それはwindow.appとしてアクセス可能になります)。テストは次のようになります。インターンはあなたのapp.print関数を呼び出すことができるようにするには

suite('app',() => { 
    test('print',() => { 
     const result = app.print('Smith'); 
     assert.equal(result, 'hello Mufasa Smith'); 
    }) 
}); 

、アプリケーションのスクリプトがロードする必要があります。あなたのコードがブラウザ用であると仮定すると、テストスイートでInternのloadScript関数を使うか、スクリプトをプラグインとして読み込むことができます。

suite('app',() => { 
    before(() => { 
     return intern.loadScript('path/to/script.js'); 
    }); 

    // tests 
}); 

または

// intern.json 
{ 
    "plugins": "path/to/script.js" 
    // rest of config 
} 
+0

あなたのジェイソンありがとうございます。完璧に動作します – Neil

関連する問題