酶mocha AssertionError:预期0到21等

时间:2016-12-05 00:00:16

标签: node.js reactjs mocha chai enzyme

为应用编写一些单元测试并在describe块中敲击墙。

/* 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)开始,但我认为没有错。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

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

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