首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试与摩卡和柴反应

测试与摩卡和柴反应
EN

Stack Overflow用户
提问于 2018-01-08 14:03:20
回答 1查看 709关注 0票数 1

我有一个简单的javascript计算器应用程序,我想学习使用Mocha和柴的测试驱动开发。

calculator_app.js

代码语言:javascript
复制
class calculator_app extends React.Component {
    constructor(props) {
      super(props);
      this.state = {
        formdata: ''
      };

      this._onAdd  = this._onAdd.bind(this);
      this._onSubtract  = this._onSubtract.bind(this);
      this._onMultiply  = this._onMultiply.bind(this);
      this._onDivide = this._onDivide.bind(this);
      this._onClear = this._onClear.bind(this);
      this.sayHello = this.sayHello.bind(this);
    }

    sayHello() {
        return 'hello';
    }

    render() {

        return (
            <App>
                {infoform}
                <MyHeader/>
                <Section>
                <Box colorIndex="brand">
                    <Heading style={{color: '#ffffff'}} id='header1'>Frosties Calculator</Heading>
                </Box>
                    <Box alignSelf="center" />
                    <Box>
                        <Box>
                            <Header size="small" colorIndex="grey-2" id='header2' />
                            <Box colorIndex="light-2" pad={{ horizontal: 'medium', vertical: 'medium', between: 'medium' }}>
                                <Box colorIndex="light-1" pad="large" separator="all">
                                    <Heading strong={true} id='header3'>Calculator</Heading>    
                                    <Paragraph>
                                        Sample code to display a calculator. 
                                    </Paragraph>                                
                                </Box>
                            </Box>
                        </Box>
                    </Box>
                </Section>
                <MyFooter/>
            </App>
        );
    }
}

export default calculator_app;

calculator_app.test.js

代码语言:javascript
复制
const assert = require('chai').assert;
const calculator = require('../src/js/components/calculator_app.js');

describe('calculator', function () {
  describe('sayHello()', function () {

        it('app should return hello', function () {
          let result = calculator.sayHello();
          assert.equal(result, 'hello');
        });
    });
});

当我尝试运行calculator_app.test来检查sayHello函数的返回时,我会得到以下错误:

0传递(107 is )1失败1)计算器sayHello()应用程序应该返回hello: TypeError: calculator.sayHello不是上下文中的函数。(C:/Users/maherni/Desktop/Projects/JavaScript/calculator_app/test/calculator_app.test.js:8:29)

有谁能告诉我我做错了什么,我已经检查了我的测试文件的路径和外观。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-08 14:17:08

看起来,您试图调用方法sayHello,而不实例化calculator React组件类。试试这个:

代码语言:javascript
复制
    it('app should return hello', function () {
      let result = new calculator().sayHello();
      assert.equal(result, 'hello');
    });

如果您想测试render方法的行为,那么确实应该看看,就像保罗·菲茨杰拉德建议的那样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48151925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档