我有一个称为仪表板的组件。在componentDidMount生命周期方法中,我正在调用异步帮助器方法。现在在单元测试中,我试图对同一方法进行存根以验证方法调用和响应
我已经尝试过sinon.stub在安装组件之前将方法存根
import React from 'react';
import { getEvents} from '../actions';
class Dashboard extends React.Component {
//constructor
componentDidMount() {
getEvents().then(response => {
//does something
});
//other methods
}
import * as actions from '../../actions';
import sinon from 'sinon';
import Dashboard from '../index';
import { mount } from 'enzyme';
import { expect } from 'chai';
describe('testing dashboard', () => {
it('should call get events helper method', () => {
const stub = sinon.stub(actions, 'getEvents').returns(Promise.resolve({}));
const wrapper = mount(<Dashboard/>);
expect(stub).to.have.property('callCount', 1);
})
})
断言失败,呼叫计数为零