- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试测试是否使用 enzyme 和 jest 调用了 React 组件方法。该函数应该在 <section>
时被调用。元素变得没有焦点(模糊)。该组件连接到 Redux 存储,但它也按名称导出,以将 Redux 排除在外。下面是该组件的简化版本:
export class Section extends React.Component {
constructor(props) {
super(props)
this.formRef = React.createRef();
this.submitForm = this.submitForm.bind(this);
this.state = {
formSubmitted: false
}
}
submitForm() {
console.log("form submitted");
this.setState({ formSubmitted: true })
if (this.formRef && this.formRef.current) {
this.formRef.current.submit();
}
}
render() {
return (
<section onBlur={this.submitForm}>
<form ref={this.formRef} action={url} method='post'>
<input type="text" name="something" />
</form>
</section>
);
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Section);
我为 spyOn
尝试了各种组合部分,因为它似乎是问题的根源。都失败了。
import React from 'react';
import { shallow } from 'enzyme';
import { Section } from './Section';
describe('Section', () => {
it('should submit form on blur', () => {
const wrapper = shallow(<Section content={{ body: [] }} />);
const spy = spyOn(wrapper.instance(), 'submitForm');
const spy2 = spyOn(Section.prototype, 'submitForm');
const spy3 = jest.spyOn(wrapper.instance(), 'submitForm');
const spy4 = jest.spyOn(Section.prototype, 'submitForm');
wrapper.find('section').simulate('blur');
expect(wrapper.state().formSubmitted).toEqual(true);
expect(spy).toHaveBeenCalled();
expect(spy2).toHaveBeenCalled();
expect(spy3).toHaveBeenCalled();
expect(spy4).toHaveBeenCalled();
})
})
除了测试expect(...).toHaveBeenCalled
,我还给了组件一个状态并测试了它为了验证函数是否真的被调用了,看起来确实如此。
console.log('form submitted')
出现在控制台中。
测试expect(wrapper.state().formSubmitted).toEqual(true);
通过,这向我表明正在调用正确的函数。但是,我不想为了测试而出现不必要的状态。刚刚添加了状态以断言正在调用“submitForm”方法。
断言 expect(...).toHaveBeenCalled()
全部失败并显示错误 Expected spy to have been called, but it was not called
或 Expected mock function to have been called, but it was not called.
最佳答案
有趣的是,这个问题直接深入到 JavaScript 的一些更不寻常的方面。
submitForm
最初定义为 prototype method :
class Section extends React.Component {
...
submitForm() { ... }
...
}
...这意味着您将像这样创建 spy
:
jest.spyOn(Section.prototype, 'submitForm');
...但随后它被重新定义为 instance property在构造函数
中:
this.submitForm = this.submitForm.bind(this);
...意味着您将现在像这样创建 spy
:
jest.spyOn(wrapper.instance(), 'submitForm');
...但是 仍然 不起作用,因为 onBlur
被直接 绑定(bind)到 this.submitForm
在渲染
期间:
<section onBlur={this.submitForm}>
因此,当前编写代码的方式实际上使得无法监视 submitForm
,因为创建 spy
需要一个 instance
但 instance
在组件呈现之前不可用,并且 onBlur
在呈现期间直接绑定(bind)到 this.submitForm
。
将 onBlur
更改为 调用 this.submitForm
的函数,以便每当 onBlur
触发时它都会调用this.submitForm
的当前值:
render() {
return (
<section onBlur={() => this.submitForm()}>
<form ref={this.formRef} action={url} method='post'>
<input type="text" name="something" />
</form>
</section>
);
}
...然后当您将 submitForm
替换为 instance
上的 spy
时,spy
将被调用当 onBlur
触发时:
describe('Section', () => {
it('should submit form on blur', () => {
const wrapper = shallow(<Section content={{ body: [] }} />);
const spy = jest.spyOn(wrapper.instance(), 'submitForm');
wrapper.find('section').simulate('blur');
expect(wrapper.state().formSubmitted).toEqual(true);
expect(spy).toHaveBeenCalled(); // Success!
})
})
关于javascript - 即使调用了 React 组件方法,expect(...).toHaveBeenCalled() 也会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55227855/
我有一个函数,它只是创建一个写入流并写入它...我想测试该函数,并测试 createWriteStream 的 write() 函数> 被调用。但是,我正在 mock fs 模块,因为我不想在测试时实
据我所知, Jasmine 的 .toHaveBeenCalled() Matcher 返回一个 Promise,该 Promise 在函数被调用时被解析。实际上对我来说,它返回未定义: it('sh
什么时候应该调用这些方法中的每一个,为什么? const spyStart = spyOn(el, 'func1'); expect(spyStart).toHaveBeenCalled(); con
我正在使用 Composition API 和 setup() Hook 运行 vue3 应用程序。 我使用 Vitest 作为单元测试框架。 (v 0.6.1) 我有以下示例组件: // src/c
我在进行这个简单的测试时遇到了很多麻烦。 我在想要测试的 Controller 中有一个 $scope.$on 监听器。我只是想确保它在广播事件之后被调用。 为此,我认为以下代码可以工作: descr
我正在使用 Composition API 和 setup() Hook 运行 vue3 应用程序。 我使用 Vitest 作为单元测试框架。 (v 0.6.1) 我有以下示例组件: // src/c
谁能告诉我为什么以下测试失败。 var Person = function() {}; Person.prototype.helloSomeone = function(toGreet) { re
我正在使用 Enzyme/Jest 为容器帽子上的函数编写测试,该函数是通过复选框组件的 onChange 触发的。我正在尝试模拟“更改”,但是,“更改”以某种方式没有触发调用 onChange 函数
这是测试代码 var list = new List([1, 2, 3, 4]); var list2 = new List([5, 1]); beforeAll(function () { sp
我在 React 中有一个小组件,它在渲染时生成两个随机数,然后要求用户提交这些数字的总和,如果它们正确,则增加他们的分数。 以下代码处理此游戏并在浏览器中按预期运行: import React, {
我是 jasmine 的新手,这是我的 src 文件,我在其中创建了 Auth 类 function Auth() { } Auth.prototype.isEmpty = function(str)
使用 Jest 和 React,我如何监视一个 this.setState()并测试功能 hasBeenCalled() it('shall update targets when engine re
我正在尝试测试是否使用 enzyme 和 jest 调用了 React 组件方法。该函数应该在 时被调用。元素变得没有焦点(模糊)。该组件连接到 Redux 存储,但它也按名称导出,以将 Redux
被测代码: module lib { export class Topic { private _callbacks: JQueryCallback; publ
我目前在监视 typescript 类中调用的继承方法时遇到问题,其中 toHaveBeenCalled() 方法返回 false,即使被监视的方法已被调用。看看下面的场景... 我有两个类,用 Ty
我正在尝试对返回 promise 的函数进行单元测试。我在验证是否模拟函数被调用。这是我所做的。, // codetotest.js const { SomeModule, isSomething,
我的监视方法的示例失败,并显示“预期 spy handle_click已被调用”。当它应该过去的时候。但是,我收到控制台日志“Foo handle_click called!”,所以我知道它正在被调用
一个简单的组件: {{ count }} Increment export default { data () { r
我正在尝试 stub /监视翻译,而不仅仅是 mock 它,即使在这种最基本的情况下,我似乎也无法触发它。 /** * ComponentName.jsx */ import { useTrans
我是一名优秀的程序员,十分优秀!