【发布时间】:2019-01-01 11:12:07
【问题描述】:
我正在做一些单元测试,但我一直在尝试存根这个方法。 我也在使用 materialize 进行下拉,我认为这是相关的。 有人遇到过这个问题并可以提供帮助吗?
我的代码去
componentDidUpdate() {
$('.dropdown-trigger-filter').dropdown({belowOrigin: true, alignment: 'right', closeOnClick: false});
this.controller.isFirstLoginRedirect(this.props);
}
我的测试:
describe('Home Component', async () => {
let container = props => { return <Home store={store} {...props} />; };
it('should render correctly', () => {
const props = {};
const wrapper = mount(shallow(container(props)).get(0));
const tree = renderer.create(container(props)).toJSON();
expect(tree).matchSnapshot();
});
it('should change state of selected tab to quotes ', () => {
const props = {};
const wrapper = mount(shallow(container(props)).get(0));
const $ = sinon.stub();
$.withArgs('.dropdown-trigger-filter').returns({});
(wrapper.find('.tab').at(0)).simulate('click');
expect(wrapper.state('selectedTab')).equal('##quotes');
expect(wrapper.debug()).matchSnapshot();
});
});
错误:
TypeError: $(...).dropdown is not a function
【问题讨论】:
-
这不是一个函数。您正在返回一个没有任何内容的存根
{}。现在如果你返回{dropdown:function(){}}那么它就会存在,:) -
这确实有效!谢谢!
标签: javascript jquery unit-testing sinon