【发布时间】:2021-04-30 02:35:10
【问题描述】:
我正在为单击增量按钮编写一个测试用例。
function App() {
const [count, setCount] = useState(0);
const handleIncrement = () => {
if(count >= 0){
setCount(count + 1);
}
}
const handleDecrement = () => {
if(count > 0){
setCount(count - 1);
}
}
return (
<div>
Count : {count}
</div>
<div>
<button onClick={handleIncrement}>Increment</button>
<button onClick={handleDecrement}>Decrement</button>
</div>
</div>
);
}
下面是给我错误的测试用例Matcher error: received value must be a mock or spy function
当点击事件被触发时,如何期望按钮被点击。有人可以帮忙吗?
it('should handle count increment', ()=>{
render(<App />);
const incrementButton = screen.getByRole('button',{name: 'Increment'})
fireEvent.click(incrementButton)
expect(incrementButton).toHaveBeenCalled()
})
【问题讨论】:
-
该测试没有意义 - 模拟点击不会调用按钮。按钮的 onClick 处理程序被调用,但这只是一个实现细节;测试相关状态更新的可见结果。
标签: javascript reactjs jestjs react-testing-library