【问题标题】:iterating over array with several objects使用多个对象迭代数组
【发布时间】:2016-03-17 02:55:37
【问题描述】:

这里的格式很好...在堆栈中尝试过,但效果不佳...https://gist.github.com/js08/22654f6cd2f160aa2e0a

  • 我是 mocha 测试的新手..
  • 现在它使用数组中的单个对象。
  • 如何使其适用于数组中的多个对象。
  • 我不知道如何迭代它
  • 在下面提供我的代码
  • 数组中的单个对象:

    shallowRenderer.render(<SportsList  disclosures ={[ 'id': 'foo', 'text': 'car racing' ]} />);
    
  • 数组中的多个对象:


shallowRenderer.render(<SportsList  disclosures ={[ {id: 100, text: 'car racing'}, {id: 10, text: 'hello vworld'}, {id: 100, text: 'car racing'} ]} />);

import {expect} from 'chai';
import React from 'react';
import TestUtils from 'react-addons-test-utils';
import SportsList from '../../src/components/footer/disclosure-list.jsx';
import initializeJsDom from '../test-utils/dom.js';
import {getGridLayoutClasses} from 'sports-css-grids';


describe('shallow renderer tests for disclosure-list ', function() {
    let shallowRenderer = TestUtils.createRenderer();


    it('should render correctly', () => {

        shallowRenderer.render(<SportsList  disclosures ={[ {id: 100, text: 'car racing'}, {id: 10, text: 'hello vworld'}, {id: 100, text: 'car racing'} ]} />);
        //shallowRenderer.render(<SportsList  disclosures ={[ 'id': 'foo', 'text': 'car racing' ]} />);
        let sportsOutputElement = shallowRenderer.getRenderOutput();

        let sportsTitle = sportsOutputElement.props.children[0].key;

        let text = sportsOutputElement.props.children[0].props.dangerouslySetInnerHTML.__html;

       //result succeful for single value 
        expect(sportsTitle).equal('100');
       //result succeful for single value
        expect(text).equal('car racing');
    });



 });

json structure
renderedElement - multiple arrays-- -- > 

{
  "type": "div",
  "key": null,
  "ref": null,
  "props": {
    "className": "pageFooter-disclosures",
    "children": [{
      "type": "p",
      "key": "100",
      "ref": null,
      "props": {
        "dangerouslySetInnerHTML": {
          "__html": "hello world"
        }
      },
      "_owner": null,
      "_store": {}
    }, {
      "type": "p",
      "key": "10",
      "ref": null,
      "props": {
        "dangerouslySetInnerHTML": {
          "__html": "hello vworld"
        }
      },
      "_owner": null,
      "_store": {}
    }, {
      "type": "p",
      "key": "100",
      "ref": null,
      "props": {
        "dangerouslySetInnerHTML": {
          "__html": "hello world"
        }
      },
      "_owner": null,
      "_store": {}
    }]
  },
  "_owner": null,
  "_store": {}
}√
should render correctly(3 ms)

【问题讨论】:

    标签: javascript jquery html reactjs mocha.js


    【解决方案1】:

    使用来自chaiexpect,您可以使用 eql 来测试对象的深度相等性

    expect(sportsOutputElement.props.children).to.eql([ {id: 100, text: 'car racing'}, {id: 10, text: 'hello vworld'}, {id: 100, text: 'car racing'} ]);
    

    【讨论】:

    • 感谢您的留言...您能否更新我的代码...令人困惑
    • 将代码中以//result successfull for single value开头的四行替换为上面的行
    • 感谢您的回复....但我收到一个错误 AssertionError: expected [ Array(3) ] to deep equal [ Array(3) ] + expected - actual
    • 似乎数组可能出现故障。如果是这种情况,请使用 deep.include.members 而不是 eqls 像这样 expect(sportsOutputElement.props.children).to.deep.include.members([ {id: 100, text: 'car racing'}, {id: 10, text: 'hello vworld'}, {id: 100, text: 'car racing'} ]);
    • 我仍然收到另一个错误 AssertionError: expected [ Array(3) ] to be a superset of [ Array(3) ]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    相关资源
    最近更新 更多