Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
React native Can';无法使用react本机测试库查找命名元素_React Native_Unit Testing_React Testing Library_React Native Testing Library - Fatal编程技术网

React native Can';无法使用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

我试图通过占位符文本获取元素,但react本机测试库不断向我抛出相同的错误:

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');