Javascript 如何检查函数是否有参数以及它是否';s号

Javascript 如何检查函数是否有参数以及它是否';s号,javascript,unit-testing,mocha.js,chai,Javascript,Unit Testing,Mocha.js,Chai,我不熟悉单元测试和使用摩卡/柴。我试图测试函数是否有参数,是否是数字 // Main Function function Sh(partnerUserId) { function validPartnerId(partnerUserId) { if (!partnerUserId); throw new Error("Missing partnerId"); if (isNaN(partnerUserId)); throw new Error("Not

我不熟悉单元测试和使用摩卡/柴。我试图测试函数是否有参数,是否是数字

// 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(){
       ????
    });

});

如果有更好的方法,我愿意接受建议。我试图找到如何捕获参数的值并在单元测试中验证它

使用类似以下内容:

function isNumber(x) {
  return typeof x === 'number';
}
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);    
  });
});
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");
    });
});
然后,您可以在其他地方使用
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 ...
}

单元测试建议:

创建如下所示的对象:

function isNumber(x) {
  return typeof x === 'number';
}
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);    
  });
});
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");
    });
});
下面是我发现的一篇文章,它给出了一个小例子:


希望这对您有所帮助

您的
Sohalo
功能的用途还不清楚。我必须重写它才能得到一个有意义的函数。无论如何,您需要使用
expect(fn).to.throw(…)
来检查检查是否正在进行。它的文档是。请注意,当您想检查具有特定参数的调用是否引发异常时,最方便的方法是使用。比如:

expect(Sohalo.bind(undefined, 1)).to.not.throw(Error, "Not a number");
将测试呼叫
Sohalo(1)
。(bind的第一个参数在函数中设置this的值,这里不需要它,所以我将它保留为未定义的

因此,包含函数并对其进行测试的文件可能如下所示:

function isNumber(x) {
  return typeof x === 'number';
}
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);    
  });
});
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
只是为了说明。

如果用户传入
NaN
@JuanMendes,这将失败。事实上,尽管NaN代表“not-A-Number”,它实际上只是指一个不能在数字类型限制内指定的值(例如除以0)。NaN的实际
typeof
返回“number”。因此,它实际上是类型
number
。更多信息请点击此处:。所有这些都说明了,这取决于您希望如何实现事情。如果您不希望
NaN
为数字类型检查返回true,那么为此添加一个额外的检查。是的,因为
typeof NaN
'number'
,OP希望它失败,而您的代码不会这样做。`如果您传入
number(5)
,它也会失败,但不应该?嗯,当我按照您描述的方式运行它时,它可以工作:。另外,我也不知道为什么需要像这样将一个原语包装成它的对象类型。我的意思是
newnumber(2)
。没有很好的理由这么做,但是有可能一些代码会这么做,所以你应该防止它,OP正试图对混乱的家伙进行额外的防御。我说的是单元测试。我使用js代码,我需要单元测试方面的帮助。如果通过
0