我也在用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');期待您的回音,谢谢!
发布于 2016-12-07 16:28:06
我认为我们可以通过这种方式将函数存根到您正在测试的组件中。
sinon.stub(ContentEditor.prototype, 'getUsers')并像这样测试它
expect(ContentEditor.prototype.getUsers.called).to.be.true;https://stackoverflow.com/questions/40512527
复制相似问题