Javascript Jest TypeError:this.props.fetchBitcoin不是一个函数
我正在运行一个带有Jest的测试,以便在测试中运行,该测试应该使用redux dispatches检查函数是否通过Javascript Jest TypeError:this.props.fetchBitcoin不是一个函数,javascript,reactjs,redux,jestjs,Javascript,Reactjs,Redux,Jestjs,我正在运行一个带有Jest的测试,以便在测试中运行,该测试应该使用redux dispatches检查函数是否通过props传入 以下是测试: describe("when mounted", () => { beforeEach(() => { props.fetchBitcoin = mockFetchBitcoin; loot = mount(<Loot {...props} />); }); it("fires
props
传入
以下是测试:
describe("when mounted", () => {
beforeEach(() => {
props.fetchBitcoin = mockFetchBitcoin;
loot = mount(<Loot {...props} />);
});
it("fires the `fetchBitcoin()` from the props", () => {
expect(mockFetchBitcoin).toHaveBeenCalled();
});
});
接收道具的组件如下所示:
import React from "react";
import { Component } from "react";
import { connect } from "react-redux";
import { fetchBitcoin } from "../actions/bitcoin";
export class Loot extends Component {
render() {
return <h3>Bitcoin balance:</h3>;
}
componentDidMount() {
this.props.fetchBitcoin();
}
}
export default connect(state => state, { fetchBitcoin })(Loot);
import { FETCH_BITCOIN } from "./constants";
export const fetchBitcoin = () => {
return dispatch => {
return fetch("https://api.coindesk.com/v1/bpi/currentprice.json")
.then(response => response.json())
.then(json => dispatch({ type: FETCH_BITCOIN, bitcoin: json }));
};
};
我也遇到过同样的问题 对我来说,解决方案就是将if语句添加到React组件中,如下所示:
export class Loot extends Component {
render() {
return <h3>Bitcoin balance:</h3>;
}
componentDidMount() {
if (this.props.fetchBitcoin) {
}
}
导出类战利品扩展组件{
render(){
返回比特币余额:;
}
componentDidMount(){
if(this.props.fetch比特币){
}
}
你能分享一下mockFetchBitcoin
的定义吗?mockFetchBitcoin=jest.fn();你有没有尝试过引用mock而不是覆盖props键:就像以前一样(()=>{loot=mount();});
export class Loot extends Component {
render() {
return <h3>Bitcoin balance:</h3>;
}
componentDidMount() {
if (this.props.fetchBitcoin) {
}
}