2016-10-18 12 views
0

私は(私は「古い学校」の道を好む、あなたは何をしたいunmockの大ファンではないM)、jest.mockにスパイの冗談モック

iがmこの道をスパイをしようとしてmを次の試験持つ:

jest.mock('node-fetch'); 
// ... 
it('should have called the fetch function wih the good const parameter and slug', done => { 
      const slug = 'slug'; 
      const stubDispatch =() => null; 
      const dispatcher = fetchRemote(slug); 
      dispatcher(stubDispatch).then(() => { 
       expect(???).toBeCalledWith(Constants + slug); 
       done(); 
      }); 
     }); 

をそして、これは私が(それが完了していない、テスト駆動)をテストするコードです:

export const fetchRemote = slug => { 
    return dispatch => { 
     dispatch(loading()); 
     return fetch(Constants.URL + slug) 
    }; 
}; 

フェッチの私のモック実装(実際にはリンパ節転移ありフェッチ):

export default() => Promise.resolve({json:() => []}); 

モックは正常に動作し、通常の実装によく置き換わります。

私の主な質問は、どうすればその嘲笑された機能をスパイできますか?私はそれが良いパラメータで呼ばれていることをテストする必要があり、私は絶対にそれを作る方法を知らない。テスト実装では、 "???"私は関係スパイを作成する方法を知らない。

あなたのモック実装で

答えて

3

、あなたが今

const fetch = jest.fn(() => Promise.resolve({json:() => []})); 
module.exports = fetch; 

を行うことができ、あなたのテストであなたは

const fetchMock = require('node-fetch'); // this will get your mock implementation not the actual one 
... 
... 
expect(fetchMock).toBeCalledWith(Constants + slug); 

を行う必要があり、これは

を役に立てば幸い
関連する問題