测试函数参数的数据类型的正确性

Test for correctness of datatypes of function arguments

本文关键字:数据类型 正确性 参数 函数 测试      更新时间:2023-09-26

我正在使用断言类型模块来测试函数参数的数据类型的正确性。

我所做的示例代码;

var cs = require("coffee-script/register");//this line needed to require("assert-type"). Some bug.
var ty = require("assert-type"); //https://github.com/mlin/node-assert-type
var T = ty.Assert;
function test_func(file_name, start_time, end_time) {
    T(ty.str, ty.obj.not.null, ty.obj.not.null)(file_name, start_time, end_time);
    //action code
}

如果我使用chaijs模块,相应的代码是什么?http://chaijs.com/

我浏览了文档,但没有找到相应的代码。我应该坚持使用断言类型模块吗?

使用chaijs类型检测。https://github.com/chaijs/type-detect

var type = require('type-detect');
assert(type(file_name) === "string");
assert(type(start_time) !== "null");
assert(type(end_time) !== "null");