2017-02-07 6 views
1

私はmocha + sinon.js + chaiを使用しています。 私はたくさんのスタブやモックで多くのテストをしていますので、it()関数は長すぎます。 他のファイル/ configファイルで私のスタブを整理する方法はありますか? 各テストが異なる方法でスタブしているため(スタブは同じオブジェクトですが、動作が異なるため)、スタブをbefore()に配置することはできません。sinon.js - 私のスタブを整理するには?

いいアイデアですか?

TNX :)

答えて

0

あなたの応答のためのJSONをロードするためにbabel-plugin-inline-jsonを使用することができます。
configure babel-plugin-inline-jsonをインストールします。

//.babelrc: 
{ 
    "plugins": [["inline-json", {"matchPattern": "config"}]] 
} 

のような外部ファイルにあなたの応答を定義します。あなたのテストでそれを使用し

//res.config.json 
{ 
    "foo": "bar" 
} 


const expect = require('chai').expect; 
const sinon = require('sinon'); 

class SomeObject { 
    doSomething(){ 
     return null; 
    } 
} 

describe('Some tests:', function() {  
     it.only('With fake response',() => { 
     const obj = require('./res.config'); 
     const someObject = new SomeObject(); 
     sinon.stub(someObject,'doSomething').returns(obj); 

     const res = someObject.doSomething(); 

     expect(res).to.haveOwnProperty('foo'); 
     }); 

    }); 
}); 

あなたは、いくつかのconfig.jsonファイルを持つことができます。

関連する問題