React native Can';无法使用react本机测试库查找命名元素
我试图通过占位符文本获取元素,但react本机测试库不断向我抛出相同的错误:React native Can';无法使用react本机测试库查找命名元素,react-native,unit-testing,react-testing-library,react-native-testing-library,React Native,Unit Testing,React Testing Library,React Native Testing Library,我试图通过占位符文本获取元素,但react本机测试库不断向我抛出相同的错误: expect(received).toEqual(expected) // deep equality Expected: "Cirurgião" Received: {"_fiber": {"_debugHookTypes": null, "_debugID": 451, "_debugIsCurrentlyT
expect(received).toEqual(expected) // deep equality
Expected: "Cirurgião"
Received: {"_fiber": {"_debugHookTypes": null, "_debugID": 451, "_debugIsCurrentlyTiming":
false, "_debugNeedsRemount": false, "_debugOwner": [FiberNode], "_debugSource": [Object],
"actualDuration": 0, "actualStartTime": -1, "alternate": null, "child": [FiberNode],
"childExpirationTime": 0, "dependencies": null, "effectTag": 129, "elementType": [Function
Component], "expirationTime": 0, "firstEffect": null, "index": 0, "key": null, "lastEffect":
null, "memoizedProps": [Object], "memoizedState": null, "mode": 0, "nextEffect": null,
"pendingProps": [Object], "ref": [Function ref], "return": [FiberNode], "selfBaseDuration": 0,
"sibling": null, "stateNode": [Component], "tag": 1, "treeBaseDuration": 0, "type": [Function
Component], "updateQueue": null}}
下面是我要测试的代码:
const { getByTestId, queryByTestId, getByPlaceholderText} = render(
<Input placeholder="Cirurgião"
testID='input_buscaCirurgiao_index'
value={mockValue}
autoCorrect={false}
keyboardType={(Platform.OS === 'android') ? 'visible-password' : 'default'}
onChangeText={mockState}
onSubmitEditing={mockState}
autoCapitalize='none' />
);
expect(getByPlaceholderText('Cirurgião')).toEqual('Cirurgião');
const{getByTestId,queryByTestId,getByPlaceholderText}=render(
);
expect(getByPlaceholder文本('Cirurgião')).toEqual('Cirurgião');
我还尝试通过getByTestId和queryByTestId获取我要测试的元素,两者都有类似的错误
如果我尝试使用waitFor()=>进行测试,我不会得到任何错误消息,即使我尝试更改预期的输出,如expect(getByTestId('test1')。toEqual('test2');)
它运行平稳,但不应该如此
我不确定我做错了什么。
getByPlaceholderText
返回值。实际上,它成功地这样做了。节点表示为一个对象,您的测试显示
Expected: "Cirurgião"
Received: {"_fiber": { //<- Here you actually received an object representing the node
您可能需要测试两种可能的情况:
getByPlaceholder文本('Cirurgião')
如果渲染树中没有组件,它将抛出,因此测试将失败
第二个测试将是:
const input=GetByPlaceholder文本('Cirurgião');
expect(input.props.placeholder).toBe('Cirurgião');
我相信你真正想要的是期望(getByPlaceholderText('Cirurgião')).toBeInTheDocument();
。这将测试您的
元素是否呈现了placheholder文本'Cirurgião'
。非常感谢您的回答。它对我有效,尝试了使用其他道具(如testID或value)进行测试,效果也一样。
.toEqual('Cirurgião');