2016-11-09 222 views
1

所以我測試與摩卡+酶+ Sinon React應用程序。我想用if語句來測試一個函數,其中有一個調用另一個函數。 我的目標是輸入if語句,但保留第二個函數調用。這裏是代碼:Sinon Stub功能在另一個功能

onSearchChange = ({value}) => { 
    const port = '/search-users?search_q='; 
    const path = [port, value].join(''); 

    if (value.length >= 2 && value.length <= 5) { 
     this.getUsers(path, (array) => { 
      this.setState({ suggestions: fromJS(array) }) 
     }); 
    } 
}; 

所以我想輸入if語句,但不能調用getUsers()函數。我怎樣才能做到這一點? 我刺探onSearchChange()這樣的:

const wrapper = shallow(<ContentEditor />); 
const spy = sinon.spy(wrapper.instance(), 'onSearchChange'); 

期待聽到的,謝謝!

+0

你不應該在對象上模擬或存根方法你測試。在你的情況下,我假設'getUser'產生了一些Ajax請求,所以存根請求而不是操縱你的測試類。 –

回答

0

我認爲我們可以用這種方式在測試的組件中存儲函數。

sinon.stub(ContentEditor.prototype, 'getUsers') 

,並測試它像

expect(ContentEditor.prototype.getUsers.called).to.be.true;