gpt4 book ai didi

node.js - enzyme Mocha 断言错误: expected 0 to equal 21

转载 作者:太空宇宙 更新时间:2023-11-04 00:30:55 24 4
gpt4 key购买 nike

为应用程序编写一些单元测试,但在描述 block 中遇到了困难。

/* eslint-env mocha */
const React = require('react')
const chai = require('chai')
const { expect } = chai
const Search = require('../js/Search')
const ShowCard = require('../js/ShowCard')
const enzyme = require('enzyme')
const { shallow } = enzyme
const data = require('../public/data')

describe('<Search />', () => {
it('should render as many shows as there are data for', () => {
const wrapper = shallow(<Search />)
expect(wrapper.find(ShowCard).length).to.equal(data.shows.length)
console.log(wrapper.debug())
})
})

搜索组件中的代码渲染 ShowCard 如下:

<div className='shows'>
{data.shows
.filter((show) => `${show.title} ${show.description}`.toUpperCase().indexOf(this.state.searchTerm.toUpperCase()) >= 0)
.map((show, index) => (
<ShowCard {...show} key={index} id={index} />
))}
</div>

(wrapper.find(ShowCard).length) 应该等于 (data.shows.length),但它给出了以下错误:

  <Search /> should render as many shows as there are data for:

AssertionError: expected 0 to equal 21
+ expected - actual

-0
+21

at Context.<anonymous> (App.spec.js:19:45)

根据上述错误,问题始于期望equal(data.shows.length),但我认为这没有任何问题。谁能指出我正确的方向?

最佳答案

哇,好尴尬啊。我将搜索构造函数的输入值的状态设置为“默认搜索词” - 从而防止出现任何搜索结果,直到从输入中手动删除该字符串为止。

用空字符串替换解决了问题。所有测试现已通过。

关于node.js - enzyme Mocha 断言错误: expected 0 to equal 21,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40965220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com