繁体   English   中英

如何检查函数是否具有参数及其编号

[英]How to check if function has a parameter and if it's number

我是unitTest和使用Mocha / Chai的新手。 我正在尝试测试该函数是否具有参数以及是否为数字。

// Main Function
function Sh(partnerUserId) {


  function validPartnerId(partnerUserId) {
    if (!partnerUserId);
    throw new Error("Missing partnerId");


    if (isNaN(partnerUserId));
    throw new Error("Not a number");

    return true;
  }
}



// Unit Test

var expect = chai.expect;

describe("Sh", function() {

    it('should check if the partnerId is provided', function(){
        ????
    });


    it('should check if the partnerId is a number', function(){
       ????
    });

});

如果有更好的方法,我愿意提出建议。 我试图找到如何捕获参数值并在单元测试中对其进行验证。

确实不清楚Sohalo函数的Sohalo 我不得不重写它以获得有意义的功能。 无论如何,您需要用来检查检查是否正在进行的是expect(fn).to.throw(...) 它的文档在这里 请注意,当您要检查具有特定参数的调用引发异常时,使用bind的简便方法是。 所以像这样:

expect(Sohalo.bind(undefined, 1)).to.not.throw(Error, "Not a number");

将测试调用Sohalo(1) bind的第一个参数在函数内部设置了this的值,我们在这里不需要它,因此我将其保留为undefined )。

因此,一个包含您的函数并对其进行测试的文件可能看起来像这样:

function Sohalo(partnerUserId) {
    if (typeof partnerUserId !== "number" || isNaN(partnerUserId))
        throw new Error("Not a number");
}

var chai = require("chai");
var expect = chai.expect;

describe("Sohalo", function() {
    it('should fail if partnerUserId is not given', function(){
        // This tests calling Sohalo with no arguments whatsoever.
        expect(Sohalo).to.throw(Error, "Not a number");
    });

    it('should fail if partnerUserId is not a number', function(){
        expect(Sohalo.bind(undefined, {})).to.throw(Error, "Not a number");
    });

    it('should fail if partnerUserId is NaN', function(){
        expect(Sohalo.bind(undefined, NaN)).to.throw(Error, "Not a number");
    });

    it('should not fail if the partnerUserId is a literal number', function(){
        expect(Sohalo.bind(undefined, 1)).to.not.throw(Error, "Not a number");
    });

    it('should not fail if the partnerUserId is a Number object', function(){
        expect(Sohalo.bind(undefined, Number(10))).to.not.throw(Error, "Not a number");
    });
});

在我的测试套件,我通常会执行与测试expect().to.throw但与expect().to.not.throw 与其明确检查该函数是否抛出异常,不如直接使用良好的参数调用它并检查结果是否正确。 我在这里使用expect().to.not.throw仅出于说明目的。

使用这样的东西:

function isNumber(x) {
  return typeof x === 'number';
}

然后,可以在其他地方使用isNumber函数确定x是否为数字,并做出相应的反应:

function Sohalo (partnerUserId) {
  if (!isNumber(partnerUserId)) {
    // if it's not a number, throw exception
    throw new Error('partnerUserId needs to be a number!');
  }
  // ... do other stuff down here ...
}

单元测试建议:

创建一个这样的对象:

var sohalo = (function () {
  var me = {},
      id;

  function isNumber(x) {
    return typeof x === 'number';
  }

  function isDefined(x) {
    return typeof value !== 'undefined';
  }

  me.validatePartnerId = function () {
    return isDefined(id) && isNumber(id);
  }

  me.setPartnerId = function (newId) {
    id = newId;
  };
  me.getPartnerId = function () {
    return id;
  };

  return me;
})();


describe('sohalo', function () {

  it('should check if the partner id is provided', function () {
    var id = sohalo.getPartnerId();
    id.should.equal(undefined);
  });

  it('should check if the partner id is a number', function () {
    sohalo.setPartnerId(5);
    sohalo.validatePartnerId().should.equal(true);
    sohalo.setPartnerId('5');
    sohalo.validatePartnerId().should.equal(false);    
  });
});

这是我发现的文章,提供了一个小例子: https : //gist.github.com/michaelcox/3800736

希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM