【发布时间】:2020-03-26 14:31:39
【问题描述】:
我正在使用 react-native-testing-library 来测试我的 react-native 组件。
我有一个组件(为了这篇文章的目的,它已经被过度简化了):
export const ComponentUnderTest = () => {
useEffect(() => {
__make_api_call_here_then_update_state__
}, [])
return (
<View>
__content__goes__here
</View>
)
}
这是我的(简化版)component.spec.tsx:
import { render, act } from 'react-native-testing-library';
import { ComponentUnderTest } from './componentundertest.tsx';
test('it updates content on successful call', () => {
let root;
act(() => {
root = render(<ComponentUnderTest />); // this fails with below error message
});
expect(...);
})
现在当我运行这段代码时,我得到了这个错误:
Can't access .root on unmounted test renderer
我什至现在都不知道这个错误消息是什么意思。我按照react-native-testing-library 中的文档了解如何使用act and useEffect 进行测试。
任何帮助将不胜感激。谢谢
【问题讨论】:
标签: react-native use-effect act react-native-testing-library