【问题标题】:Undefined is not a constructor during React JS unit testing [closed]在 React JS 单元测试期间,Undefined 不是构造函数 [关闭]
【发布时间】:2016-12-19 01:13:16
【问题描述】:

我是单元测试,将简要布局代码结构。 1.currency.js,它在一个对象中包含货币,如下所示:

 module.exports = {
    'United States': 'USD',
    'India': 'Rupee',
    'Mexico': 'Peso',
   }
  1. 需要currency.js 将其打印为选项列表的CurrencyField:

    const React = require('react'); const currency = require('somepath/currency');

    const CurrencyField = ({onCurrencyNameSet, currencyName}) => {
        const currencyOptions = ['Select Currency'].concat(Object.values(currency)).map((thiscurrency) =>
            <option key={thiscurrency}>{thiscurrency}</option>
            );
        return (
            <fieldset className="currency-form-group">
                <label className="currency-form-group-label">Country</label>
                <select onChange={onCurrencyNameSet} className="currency-form" value={currencyName}>
                    {currencyOptions}
                </select>
            </fieldset>
        );
    };
    
  2. 然后我有一个 PaymentForm.js,它使用货币作为其字段之一。

类 PaymentForm 扩展 React.Component {

// 一些事件。 渲染() {

  return (
    // many things
    <CurrencyField onCurrencyNameSet={this.onFieldSet('currency')} currencyName={this.state.currencyName} />
    // many other things
  )

} }

  1. 然后我有一个像这样的单元测试框架:

    describe('渲染货币时', function() { 之前(函数(){ this.wrapper = Enzyme.mount(React.createElement(PaymentForm, {一些参数})); });

  2. 问题:undefined 不是构造函数(正在评估 'Object.values(currency)')

任何想法如何解决这个问题?

【问题讨论】:

    标签: javascript reactjs mocha.js enzyme karma-mocha


    【解决方案1】:

    此语法无效:

    ['Select Currency'].concat(currency).Object.values(currency)
    

    Object.values() 是一个独立的函数调用。你不能像这样用点符号将它组合在数组中。

    你的意思是?

    ['Select Currency'].concat( Object.values(currency) )
    

    【讨论】:

    • 是的!这是一个错字
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 2022-07-04
    • 2013-03-23
    相关资源
    最近更新 更多