首页 文章

Sinon Stub函数在另一个函数中

提问于
浏览
1

所以我用Mocha Enzyme Sinon测试React应用程序 . 我试图用if语句测试一个函数,其中有一个调用另一个函数 . 我的目标是输入if语句,但是存根第二个函数调用 . 这是代码:

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');

期待听到,谢谢!

1 回答

  • 0

    我认为我们可以在你正在测试的组件内部存储函数 .

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

    并测试它

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

相关问题