我正在尝试为一个简单的 React 组件编写一个 Jest 测试,以确认当我模拟点击时已调用了一个函数。
但是,当我使用 spyOn 方法时,我不断收到 TypeError: Cannot read property 'validateOnSave' of undefined。我的代码如下所示:
选项视图.js
class OptionsView extends React.Component {
constructor(props) {
super(props);
this.state = {
reasonCode: null,
remarkCode: null,
otherCode: null,
codeSelectionIsInvalid: [false, false, false],
};
this.validateOnSave = this.validateOnSave.bind(this);
this.saveOptions = this.saveOptions.bind(this);
validateOnSave() {
const copy = this.state.codeSelectionIsInvalid;
copy[0] = !this.state.reasonCode;
copy[1] = !this.state.remarkCode;
copy[2] = !this.state.otherCode;
this.setState({ codeSelectionIsInvalid: copy });
if (!copy[0] && !copy[1] && !copy[2]) {
this.saveOptions();
}
}
saveOptions() {
const { saveCallback } …Run Code Online (Sandbox Code Playgroud) 创建命名管道时mkfifo()和mknod()有什么区别?
我试着搜索但是得不到满意的答案.
我参考了 Stackoverflow 上的所有可用资源来进行类似查询。但我不确定这个测试的问题是什么:
它抛出以下异常。
[main] ERROR com.example.dao.spring.TransactionDAOSpring - org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers!0 matchers expected, 2 recorded.
Run Code Online (Sandbox Code Playgroud)
以下是代码:
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyMapOf;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import com.example.dto.DisplayTwo;
import com.example.dto.DisplayOne;
import com.example.dto.DisplayThree;
public class TransactionDAOSpringTest {
TransactionDAOSpring transactionDAOSpring;
@Mock
DataSource dataSource;
@Mock
JdbcTemplate jdbcTemplate;
@Mock
SimpleJdbcCall …Run Code Online (Sandbox Code Playgroud) 我想在单击“恢复默认值”按钮时恢复所有输入字段和总计的默认值,但它不起作用。在我到目前为止的代码中,所有元素都包含在网格中。
我提到的SO问题是:
实现此功能的首选方式是什么?提前致谢!
class Test extends React.Component {
constructor(props) {
super(props);
this.state = {
code1: this.props.defaults[0],
code2: this.props.defaults[1],
code3: this.props.defaults[2],
total : 0
};
this.handleInput = this.handleInput.bind(this);
this.restoreDefaults = this.restoreDefaults(this);
}
handleInput() {
//some logic
}
updateCode = (e, k) => {
this.setState({
[`code${k}`]: e.target.value
},
() => {
this.updateTotal();
});
}
updateTotal = () => {
this.setState(prevState => ({
total: (+prevState.code1 + +prevState.code2 + +prevState.code3),
}),
() => {
if (this.state.total …Run Code Online (Sandbox Code Playgroud) 开始使用 React。我的子组件 WeatherOptionsView 中有两个下拉菜单。我试图根据从父组件 WeatherContainerView 传递的先前选定值的 prop 数组来设置它的默认值。我还想将其设置为状态,因为该值将在下拉列表的 onChange 上发生变化。
当我这样做时,用户界面崩溃了。当我打印 props 的值时,我看到 selectedCodes 为 null。我这哪里出错了?任何帮助将不胜感激。谢谢!
console.log(this.props.isFailed); //false
console.log(this.props.selectedValues); //null
Run Code Online (Sandbox Code Playgroud)
WeatherOptionsContainer.js
class WeatherOptionsContainer extends React.Component {
constructor(props) {
super(props);
this.state = { isFailed: true, isLoading: false, selectedValues: ['City1', 'City2']};
}
render() {
return (
<WeatherOptionsView
isFailed={this.state.isFailed}
isLoading={this.state.isLoading}
selectedValues={this.state.selectedValues}
/>
);
}
}
Run Code Online (Sandbox Code Playgroud)
WeatherOptionsView.js
class WeatherOptionsView extends React.Component {
constructor(props) {
super(props);
this.state = { reasonState: false, selectedValues: this.props.selectedValues };
}
render() {
const cx = classNames.bind(styles);
return (
<ContentContainer fill> …Run Code Online (Sandbox Code Playgroud)