【问题标题】:SinonJS, QUnit and jQuery test runSinonJS、QUnit 和 jQuery 测试运行
【发布时间】:2014-06-27 21:52:45
【问题描述】:

我在尝试运行测试时遇到问题。我想测试一个库,该库发出 ajax 请求并为每个方法(get、post 等)返回一个 promise。

在我的测试中,我有这个:

var a = proxy.get("test")
        .done(function () {
            ok(true, "API call - GET - Success");
        })
        .fail(function () {
            ok(false, "API call - GET - Success");
        });

$.when(a).always(function () {
        start();
});

问题是测试没有运行。奇怪的是,如果我删除这些行,它会起作用:

<script type="text/javascript" src="libs/sinon-1.9.1.js"></script>
<script type="text/javascript" src="libs/sinon-qunit-1.0.0.js"></script>

控制台没有显示任何错误,所以我不知道该怎么办。也许 sinon 正在用 jQuery 做一些奇怪的事情?有什么建议吗?

提前致谢。

【问题讨论】:

    标签: javascript jquery durandal qunit sinon


    【解决方案1】:

    我发现了问题。问题是我错过了 fakeTimers。

    所以,是这样的:

    var clock = sinon.useFakeTimers();
    
    var a = proxy.get("test")
        .done(function () {
            ok(true, "API call - GET - Success");
        })
        .fail(function () {
            ok(false, "API call - GET - Success");
        });
    
    clock.tick(50);
    
    $.when(a, b, c, d, e).always(function () {
        start();
    
        clock.restore();
    });
    

    【讨论】:

      猜你喜欢
      • 2012-10-13
      • 2012-10-10
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      相关资源
      最近更新 更多