【问题标题】:enzyme mocha AssertionError: expected 0 to equal 21酶 mocha AssertionError:预期 0 等于 21
【发布时间】:2017-04-19 07:30:30
【问题描述】:

为应用编写一些单元测试并在描述块中碰壁。

/* 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())
  })
})

Search 组件中的代码是这样渲染 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 reactjs mocha.js chai enzyme


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-11-24
      • 2020-09-21
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 2015-12-19
      相关资源
      最近更新 更多