与Date.js一起使用时,Qunit会崩溃

时间:2011-09-16 19:53:14

标签: qunit datejs

我正在使用Qunit和Date.js来测试我编写的一些API函数。这是我的代码:

asyncTest("createDeal", 4, function () {
    var okStartDate = Date.today().addDays(4),
        notOkStartDatePast = Date.today().addDays(-1),
        notOkStartDateFuture = Date.today().addDays(1),
        okEndDate,
        notOkEndDateForOkStartDate;

        okEndDate = okStartDate.addDays(8);
        notOkEndDateForOkStartDate = okStartDate.addDays(1);

    $.post(createApiUrl("deal/create/1"), {"start_date" : okStartDate, "end_date" : notOkEndDateForOkStartDate}, function(data) {
        equal(data, '{"result":"fail"}', "The expected error was thrown");
    });

    $.post(createApiUrl("deal/create/1"), {"start_date" : notOkStartDatePast, "end_date" : okEndDate }, function(data) {
        equal(data, '{"result" : "fail"}', "The expected error was thrown");
    });

    $.post(createApiUrl("deal/create/1"), {"start_date" : notOkStartDateFuture, "end_date" : okEndDate }, function(data) {
        equal(data, '{"result" : "fail"}', "The expected error was thrown");
    });

    $.post(createApiUrl("deal/create/1"), {"start_date" : okStartDate, "end_date" : okEndDate }, function(data) {
        equal(data, '{"result" : "success"}', "Params passed in were OK. Query ran OK.");

        start();
    });
});

Qunit在第一次测试时不断崩溃,告诉我:

TypeError: Object [object DOMWindow] has no method 'getTime'

并抛出asyncTest中的整个函数。我做错了什么,或者这是Qunit或Date.js中的错误?

1 个答案:

答案 0 :(得分:0)

使用Date对象或传递它们是JS(或者可能是Date?,真的不知道)似乎不喜欢的东西。我将函数调用更改为:

$.post(createApiUrl("deal/create/1"), {"start_date" : notOkStartDatePast.toISOString(), "end_date" : okEndDate.toISOString() }, function(data) {
        equal(data, '{"result" : "fail"}', "The expected error was thrown");
    });

如果有人想要解释为什么这样做有效,而我的原始代码没有。

我将暂时保留这个问题。