2017-10-06 12 views
0
  • 前提: JS ES6、NodeJS
  • テストフレームワーク:TAP
  • モッキング図書館:testdouble.js

私は、メソッドの戻り値を模擬しようとしています私のクラスのこのエラーを受け取り続ける:関数の戻り値を正しくスタブするにはどうすればよいですか?

// Imports for unit testing 
const tap = require('tap'); 
const Subject = require('../src/iTunesClient.js'); 
const td = require('testdouble'); 

let reqJson; 

// Ensure the iTunes class methods are called 
tap.test('iTunesClient class methods function as intended', (t) => { 
    t.beforeEach((ready) => { 
    reqJson = td.replace('../src/reqJson.js'); 
    ready(); 
    }); 

    t.afterEach((ready) => { 
    td.reset(); 
    ready(); 
    }); 

    t.test('iTunesClient.getData', (assert) => { 
    const callback = td.function(); 
    const subject = new Subject(); 
    subject.setTerm('abc 123'); 
    subject.setURL(); 

    td.when(reqJson.get(td.callback)).thenCallback(true); 

    subject.getData(callback); 

    td.verify(callback(true)); 
    assert.end(); 
    }); 

    t.end(); 
}); 

具体的には、この行は私の問題に関連している:

td.verify(callback(true)); 

どのようにすることができますI偽物reqJson.get()ためtrueのコールバック値ここ

not ok Unsatisfied verification on test double. Wanted: - called with (true) . But there were no invocations of the test double.

は私のテストコードですか?今、 Subject.geData()は、別のファイル reqJson.jsを呼び出す iTunesClientクラスのメソッドで、その get()メソッドを使用しています。

答えて

0

私はこの問題を最近解決したので、この質問を更新したいと考えていました。基本的に、私は2つの問題があった:

したがって

When passed td.matchers.anything(), any invocation of that test double function will ignore that parameter when determining whether an invocation satisfies the stubbing.

、私は:すべてのコールバックのリターンは項目1のためtestdouble documentationパー

値の両方reqJson機能について

  1. アカウントが
  2. アカウントパラメータコードの行を次のように調整しました:

    前:td.when(reqJson.get(td.callback)).thenCallback(true);

    後:td.when(reqJson.get(td.matchers.anything(), td.callback)).thenCallback(null, null, null);

0

あなたの例では分かりにくいですが、td.replaceに電話する前にiTunesClientが必要なようです。この場合、本物のreqJsonモジュールが必要になり、3行目にキャッシュされます。

td.replaceを呼び出す必要があります。その間にはtapiTunesClientが必要です。

関連する問題