反应酶测试,无法读取属性'有'未定义

问题描述:

我正在使用Enzyme为React写测试。反应酶测试,无法读取属性'有'未定义

我的测试是非常简单的:

import OffCanvasMenu from '../index'; 
import { Link } from 'react-router'; 

import expect from 'expect'; 
import { shallow, mount } from 'enzyme'; 
import sinon from 'sinon'; 
import React from 'react'; 

describe('<OffCanvasMenu />',() => { 
    it('contains 5 <Link /> components',() => { 
    const wrapper = shallow(<OffCanvasMenu />); 
    expect(wrapper.find(<Link />)).to.have.length(5); 
    }); 
}); 

此代码基本上是直接取自airbnb/enzyme docs,但返回错误:对我在做什么

FAILED TESTS: 
    <OffCanvasMenu /> 
    ✖ contains 5 <Link /> components 
     Chrome 52.0.2743 (Mac OS X 10.11.6) 
    TypeError: Cannot read property 'have' of undefined 

我有点不清楚与文档不同。任何指导极大的赞赏。

使用Link,而不是<Link />

describe('<OffCanvasMenu />',() => { 
    it('contains 5 <Link /> components',() => { 
    const wrapper = shallow(<OffCanvasMenu />); 
    expect(wrapper.find(Link)).to.have.length(5); 
    }); 
}); 

它出现在第一个例子中的Airbnb /酶文档:

it('should render three <Foo /> components',() => { 
    const wrapper = shallow(<MyComponent />); 
    expect(wrapper.find(Foo)).to.have.length(3); 
    }); 
+0

感谢您的回应 - 我看到与'链接'相同的错误。我正在关注文档,尽管我使用的代码导入了迈克尔杰克逊的“期望”,我不确定是否会抛出它(我也检查过文档)。 – Toby

+0

你使用了什么断言库? 试试这个 - 'console.log('length',wrapper.find(Link).length);',看看控制台中出现了什么。 –

+0

添加它会在日志中产生预期的条目 - “5”。我和Chai一起使用Karma,但与Michael Jackson的[期望](https://github.com/mjackson/expect)一起使用。 – Toby

在他们的文件酶是用柴断言,所以如果你想要使用expect(***).to.have.length(***),您需要安装chai-enzyme并使用其expect。因此,如果您使用Jest快照,它将导致expect(***).toMatchSnapshot()的问题,但是这个article将帮助您解决它,因此您可以同时执行这两个操作。

+0

谢谢 - 这对我们不识别所有不同库的语法的新手们很有帮助! –