我正在尝试使用jest和酶为反应中的容器编写单元测试。我的反应代码是这样的:
AgentForm.js
import {
resetStatusMessage,
formSetError,
formResetError,
createAgent,
updateAgent
} from "../../actions";
componentWillMount = () => {
const {
agents,
match,
push,
resetStatusMessage,
formResetError
} = this.props;
this.edit = false;
this.isNewAvatar = false;
this.agent = {};
if (match.params.id) {
this.edit = true;
this.agent = getAgent(agents, match.params.id);
if ("undefined" === typeof this.agent) {
push("/agents");
}
}
resetStatusMessage();
formResetError();
};
我的测试代码是这样的:
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import configureStore from "../../store/configureStore";
import AgentForm from "./index";
import { mount } from "enzyme";
it("should call resetStatusMessage when mounted", () => {
let mockResetStatusMessage = jest.fn(),
store = configureStore();
const wrapper = mount(
<Provider store={store}>
<AgentForm resetStatusMessage={mockResetStatusMessage} />
</Provider>
);
expect(wrapper).toBeDefined();
expect(mockResetStatusMessage).toHaveBeenCalled();
});
但测试失败并显示错误消息:
expect(jest.fn()).toHaveBeenCalled()
Expected mock function to have been called
我在这里缺少什么?
答案 0 :(得分:3)
您渲染组件很浅,这意味着它无法访问安装和卸载生命周期。使用mount包装器而不是浅包装器