我为让Jest单元测试通过而感到沮丧。
我有一个提供密码形式的类,其中包含以下内容:
// Password.jsx
...
static propTypes = {
checkPasswordRules: PropTypes.func,
content: PropTypes.object
};
...
validatePassword(password, allFields) {
const { confirmPassword = '' } = allFields;
const errors = this.props.checkPasswordRules({
password,
confirmPassword
});
return errors.password ? errors.password.friendly : undefined;
}
...
get formContent() {
...
return (
<Field
type="password"
component={this.formField}
name="password"
label={content.passwordLabel}
error={content.errorContent}
validate={this.validatePassword}
/>
);
}
...
export default reduxForm({ form: 'passwordForm' })(Password);
这是我的单元测试( Jest ):
// Password.test.js
...
it('handles validatePassword method', () => {
const allValues = { password: 'password', confirmPassword: 'password1' };
const labels = content.passwordStrengthRules;
const expected = labels.confirmMatchLabel;
const component = renderer.create(
<Provider store={store}>
<Password {...props} />
</Provider>
);
const instance = component.getInstance();
const result = instance.validatePassword(allValues.password, allValues);
expect(result).toEqual(expected);
});
我遇到的问题是,当我尝试运行Jest测试时,出现一个错误,提示instance.validatePassword
不是控制台中的函数。
我不确定为什么会这样,因为我正在使用instance
从单元测试中的component
变量中获取react-test-renderer
。上面的内容不是函数,因为它正在寻找instance
中的Provider
,这不是我想要的。但是,render
在没有Provider
的情况下对组件(测试中的 )进行了抛出错误,即组件必须包装在Provider
中!
有人知道我在哪里错吗??任何帮助将不胜感激,因为我在这里处于停滞状态...
提前谢谢!
答案 0 :(得分:1)
弄清楚了。因为在这种情况下,$ ping 8.8.8.8
connect: Network is unreachable
仅找到包装/连接的reduxForm组件的实例-instance
-我无法访问未连接的组件-Provider
的方法。
通过使用以下内容,我能够访问Password
Password
如果有人遇到同样的问题,我希望这个答案对他们有用...