【问题标题】:Test the return of functions with parameters in Mocha with Chai用 Chai 在 Mocha 中测试带参数的函数的返回
【发布时间】:2016-08-03 20:04:26
【问题描述】:

我编写了一个基本的 mocha 单元测试来测试我在 node.js 中的算法挑战。我想要一个带有 chai 库单元测试的 mocha 示例,使用插入的函数参数测试函数的返回。

// algorithm.js(函数)

var alg = function(num) {
  return num;
}

module.exports = alg;

// spec/algorithm.js(测试)

var path = require('path');
var expect = require('chai').expect;

var algorithm = require(path.join(__dirname, '..', './algorithm.js'));

describe('algorithm()', function () {
  'use strict';

  it('exists', function () {
    expect(algorithm).to.be.a('function');

  });

  /*    ******* What should this be *******     */
 it('should equal 1', function () {
    expect(algorithm.alg(1)).to.equal(1);
 });
});

我使用 yeoman test-generator 来生成节点设置。测试“alg”是否为函数的第一个测试通过,但阅读文档后我不知道第二个测试应该是什么。

【问题讨论】:

    标签: function unit-testing return mocha.js chai


    【解决方案1】:

    在应答文件中应该是:

    module.exports = function(param) { // .... };
    

    我想通了,正确的函数是expect(func).to.deep.equal(return);对于测试文件:

    var path = require('path');
    var expect = require('chai').expect;
    
    var algorithm = require(path.join(__dirname, '..', './algorithm.js'));
    
    describe('algorithm()', function () {
      'use strict';
    
      it('exists', function () {
        expect(algorithm).to.be.a('function');
    
      });
    
      /*    ******* This should be *******     */
     it('should equal 1', function () {
        var res = algorithm(1);
        expect(res).to.deep.equal(1);
     });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 2017-02-14
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多