首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >React Native和React断言错误:对象与嵌套属性不匹配

React Native和React断言错误:对象与嵌套属性不匹配
EN

Stack Overflow用户
提问于 2017-11-21 15:54:57
回答 1查看 379关注 0票数 1

我对原生、酶和jest的反应是新的。我正在尝试进行一个简单的测试,来测试子节点。(也许这是一种不正确的尝试方式)。

我的组件是:

代码语言:javascript
复制
import React, { Component } from 'react';
import { View, Text, Button, TextInput } from 'react-native';

class MyComponent extends Component {
constructor(props) {
  super(props);
}

render() {
  return (
    <View >
      <Button title="My Component"/>
    </View>
  )
}
}
export default MyComponent;

我的测试是

代码语言:javascript
复制
import React from 'react';
import { configure, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import MyComponent from '../components/MyComponent.js';

configure({ adapter: new Adapter() })  //setting up enzyme

const styles = require('../styles.js');

describe('rendering', () => {  
  it('checking View and Button exists', () => {
    let wrapper
    wrapper = shallow(<MyComponent/>); 

    expect(wrapper.find('View').children().find('Button')).toHaveProperty('title','My Component')
    });
    })
});

我收到一个错误,返回的对象与预期的不匹配:

代码语言:javascript
复制
Expected the object:
 < listing of full object...>
To have a nested property:
  "title"
With a value of:
  "My Component"

返回的对象将MyComponent显示为根视图的子级,以及属性,但它失败了。我应该以不同的方式来做这件事吗?我希望能够创建一个测试结构,最终确认View组件下的一些子组件和道具。(顺便说一句,我更喜欢使用Mocha,但我遇到了this error,我还没能解决这个问题。

EN

回答 1

Stack Overflow用户

发布于 2017-11-23 11:13:43

这另一个问题帮助我回答了我的问题https://stackoverflow.com/a/46546619/4797507 (如果我没有正确地给予信任,很抱歉)

我的解决方案是:

expect(wrapper.find('View').children().find('Button').get(0).props.title).toEqual('My Component')

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47407912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档