使用酶进行单元测试无法读取未定义的 属性 'be'
Unit testing with enzyme got Cannot read property 'be' of undefined
我的不工作测试
import React from 'react';
import {shallow, mount} from 'enzyme';
import TopNav from '../components/topNav';
it('Navbar should existed', () => {
const nav = shallow(<TopNav />);
expect(nav.find('#topNav').exists()).to.be(true);
});
我的 topNav 组件。
import { Component } from 'react';
export default class topNav extends Component {
render(){
return(
<div id="topNav"><strong className="testtest">Nav bar</strong></div>
)
}
}
我不知道这里出了什么问题。我从酶的 API 文档中获取了确切的例子。但是当我这样做时,它起作用了
it('Navbar should existed', () => {
expect(shallow(<TopNav />).is('#topNav')).toBe(true);
});
这是医生说的
http://airbnb.io/enzyme/docs/api/ShallowWrapper/exists.html
您使用的是 Jasmine 还是 Chai?你这么说
it('Navbar should existed', () => {
expect(shallow(<TopNav />).is('#topNav')).toBe(true);
});
有效,.toBe
是 Jasmine expect
断言函数。而 Chai 的等价物(我认为你没有使用)是 to.be
。这就是为什么 to
对你来说是未定义的。将您的 to.be
更改为 toBe
,它应该可以工作。
import React from 'react';
import {shallow, mount} from 'enzyme';
import TopNav from '../components/topNav';
it('Navbar should existed', () => {
const nav = shallow(<TopNav />);
expect(nav.find('#topNav').exists()).toBe(true);
});
我的不工作测试
import React from 'react';
import {shallow, mount} from 'enzyme';
import TopNav from '../components/topNav';
it('Navbar should existed', () => {
const nav = shallow(<TopNav />);
expect(nav.find('#topNav').exists()).to.be(true);
});
我的 topNav 组件。
import { Component } from 'react';
export default class topNav extends Component {
render(){
return(
<div id="topNav"><strong className="testtest">Nav bar</strong></div>
)
}
}
我不知道这里出了什么问题。我从酶的 API 文档中获取了确切的例子。但是当我这样做时,它起作用了
it('Navbar should existed', () => {
expect(shallow(<TopNav />).is('#topNav')).toBe(true);
});
这是医生说的 http://airbnb.io/enzyme/docs/api/ShallowWrapper/exists.html
您使用的是 Jasmine 还是 Chai?你这么说
it('Navbar should existed', () => { expect(shallow(<TopNav />).is('#topNav')).toBe(true); });
有效,.toBe
是 Jasmine expect
断言函数。而 Chai 的等价物(我认为你没有使用)是 to.be
。这就是为什么 to
对你来说是未定义的。将您的 to.be
更改为 toBe
,它应该可以工作。
import React from 'react';
import {shallow, mount} from 'enzyme';
import TopNav from '../components/topNav';
it('Navbar should existed', () => {
const nav = shallow(<TopNav />);
expect(nav.find('#topNav').exists()).toBe(true);
});