Javascript日期测试工具

时间:2014-12-06 23:32:13

标签: javascript testing

我在Javascript,BardyDate中编写了自己的Date()实现,除了Date的所有属性/方法之外,还有很多其他的东西。为什么我这样做确实是一个非常好的问题,但这是一个很长的故事,我会在下雨天保存它。

但我认为可爱的是能够验证它仍然像Date一样正确。我对测试套件等一无所知,但想知道如何将Javascript Date对象的任何现有测试应用到我的BardyDate以显示正确性?

非常欢迎任何建议。

1 个答案:

答案 0 :(得分:0)

我等待回复,因为我不确定没有针对javascript的日期测试套件。我想向可能了解更多的人提供疑问。

但是,据我所知,这种测试都将在浏览器构建/验证中完成。当然可以使用来自该开发领域的一些现有测试套件,但我认为这不是一项容易设置的任务。许多浏览器都有build process that you could fork(特别是你可以隔离他们的日期测试用例)。在他们的测试过程中,您必须找到Javascript Date对象的给定段,这将测试以确保w3规范兼容性。

在那时,Selenium是创建单元测试的一种非常常见且实用的方法,如果设计好的网络应用集成测试(也可以挂钩到javascript),并且能够在测试中生成漂亮的报告结果

最后,可以在this post about Javascript TDD (Test Driven Design)

上找到关于Javascript测试库的累积帖子

或者您可以执行以下操作(意图是指南而非完整解决方案 - 受到dandavis评论的启发):

var epochTS = 0;
var bd = new BardyDate(epochTS);
var d = new Date(epochTS);

Object.getOwnPropertyNames(Date.prototype).forEach(function(dateFunction){
    //in this if statement you are testing the functions like getTime() and getYear()
    if(dateFunction.indexOf("get") == 0){
       console.log("dateFunction " + dateFunction + 
           "() pass: " + (bd[dateFunction]() === d[dateFunction]()))
    }
    //in this if statement you are testing the functions like toString() and toJSON()
    if(dateFunction.indexOf("to") == 0){
       console.log("dateFunction " + dateFunction + 
           "() pass: " + (bd[dateFunction]() === d[dateFunction]()))
    }
    //then there are the 16 set methods, those you probably would want to hardcode.
    //unless you are content with passing a hard coded value in like "10" -- the 
    //issue would be bounds testing, which you would likely want to hardcode.
    //beyond the scope of this for-each loop.
})

对上述代码段的一点解释

使用Object.getOwnPropertNames(Date.prototype),您可以获得Date的所有方法,尽管Date具有属性 DontEnum see this post for more info)。< / p>

此外,您可以将每个函数字符串视为javascript对象中的键,因此d[dateFunction]()dateFunction === "toString"解释/编译为d[toString](),等同于{ {1}}

相关问题