首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对以特定格式返回的日期进行单元测试

如何对以特定格式返回的日期进行单元测试
EN

Stack Overflow用户
提问于 2020-02-25 18:21:43
回答 1查看 381关注 0票数 0

我想编写一个单元测试,以便它以特定的格式返回日期。我通过"2020-02-25“,函数返回"25-Feb-2020”。

file.js

代码语言:javascript
复制
module.exports = { 
   convertDate
};
function convertDate (date) {
  console.log(date);
    let  date1 = new Date(date);
    console.log(date1);
    let formattedDate = date1.toLocaleDateString('en-GB', {
    day: 'numeric', month: 'short', year: 'numeric'
    }).replace(/ /g, '-');
    console.log( formattedDate);
    return formattedDate;
}    

file.spec.js

代码语言:javascript
复制
const expect = require('chai').expect;
const filejs = require('./file.js');

     it.only('should return the date in 25-Feb-2020 format when I pass date in 2020-02-25' ,function () {
       let fdate = filejs.convertDate("2020-02-25");
       expect(fdate).to.equal('25-Feb-2020');

     })

当我运行测试时,测试失败。

AssertionError:预期‘2月25日,-2020年’等于‘25-2月-2020年’

EN

回答 1

Stack Overflow用户

发布于 2020-02-25 19:15:16

似乎单元测试是可以的,但问题是您的函数返回‘2月25日,-2020’。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60400947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档