如何在Mocha中使用console.log语句测试功能? [英] How in Mocha test function with console.log statement?

查看:330
本文介绍了如何在Mocha中使用console.log语句测试功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个功能:

Let's say, I have a function:

function consoleOutput(param) {
  var newParam = param * param;  
  console.log(newParam);
}

如何在Mocha中测试该功能是否可以正常工作(参数将乘以2并输出到控制台).谢谢.

How can I test in Mocha, that this function will be working correctly (param will be multiplied by two and output to the console). Thanks.

推荐答案

请看下面的代码,您将获得有关摩卡测试用例的想法

Please take a look on the below code, you will get an idea on mocha test case

var assert = require('chai').assert;

// Function to be Tested
function consoleOutput(param) {
  var newParam = param * param;  
  console.log(newParam);
  return newParam;
}

// Mocha Test Case
describe('Mocha Test Case', function() {
    it('Square Root', function(done) {
        var sqrt = consoleOutput(5);
        assert.equal(sqrt, 25);
        done();
    });
});

更新(2017年3月25日):

查看以下答案以获取最佳参数

Check out the below answers for best paratice

这篇关于如何在Mocha中使用console.log语句测试功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆