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

时间:2017-11-21 07:54:57

标签: javascript testing react-native enzyme

我是新手,对原生,酶和开玩笑做出反应。我试图让一个简单的测试工作,以测试子节点。 (也许这是一种不正确的尝试方式)。

我的组件是:

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

我的测试是

Expected the object:
 < listing of full object...>
To have a nested property:
  "title"
With a value of:
  "My Component"

我收到的错误是对象返回与预期的匹配:

com.android.tools.build:gradle:3.0.0

返回的对象将MyComponent显示为根视图的子项以及prop,但它失败了。我应该这样做吗?我希望能够创建一个测试结构,最终确认View Component下的一些子组件和道具。 (作为旁注,我更愿意使用摩卡,但我反对this error我无法解决的问题。

1 个答案:

答案 0 :(得分:0)

这个问题帮助我回答了我的问题 https://stackoverflow.com/a/46546619/4797507(如果我没有正确给予信用,我会道歉)

我可以使用解决方案:

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