Javascript 如何编写用于创建唯一ID的单元测试?
这就是我目前所拥有的。但我不知道如何编写测试来确保它正常工作Javascript 如何编写用于创建唯一ID的单元测试?,javascript,mocha.js,Javascript,Mocha.js,这就是我目前所拥有的。但我不知道如何编写测试来确保它正常工作 const uniqid = require('uniqid'); module.exports = ({ name }) => { return context => { // Generate a uniqid and add it user context.data['id'] = uniqid(name).toUpperCase(); return context; }; };
const uniqid = require('uniqid');
module.exports = ({ name }) => {
return context => {
// Generate a uniqid and add it user
context.data['id'] = uniqid(name).toUpperCase();
return context;
};
};
我正在使用eslint和mocha我假设您想测试
上下文
是否接收到适当的id
,这是一个大写的字符串
,以名称
开头,后跟18个字符A-Z
或数字
我不知道您使用的是什么测试框架,但您可以使用以下方法验证id
:
//忽略此项,只需要'uniqid'模块
const uniqid=str=>`${str}4n5pxq24kpiob12og9`;
常量addId=({name})=>{
返回上下文=>{
//生成uniqid并将其添加到用户
context.data['id']=uniqid(name.toUpperCase();
返回上下文;
};
};
//addId应在“user.data.id”中添加适当的id`
const{data:{id}}=addId({name:'Annie'})({data:{});
assert(/^ANNIE[A-Z\d]{18}$/.test(id))代码>为什么它有异步
??很抱歉,我将使用eslint和mocha删除itI am。抱歉,忘记了框架。@minhquanggo已更新。让我知道这是否有效,因为我没有实际运行它。:)哦,那是因为我把你的函数命名为;您的函数被导出为module.exports=…
,因此函数就是您导入的函数;e、 例如,const someFn=require('../someFileName')代码>。