2016-11-09 144 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;