【问题标题】:sinon stub timing out phantomjssinon存根超时phantomjs
【发布时间】:2015-01-12 00:53:39
【问题描述】:

我正在使用 qunit 和 sinonjs 对一个 jquery 插件进行单元测试。它在浏览器中运行良好,所有测试都通过了,但是当我使用 Grunt 在命令行上运行时,我收到错误“PhantomJS 超时,可能是由于缺少 QUnit 启动”。该问题是由我为 window.alert 创建的 sinonjs 存根引起的。谁能解释我的 sinon 存根有什么问题?我猜 phantomjs 正在等待响应。我尝试过 QUnit.start() 并尝试从我的 sinon 存根返回 true/false/undefined。

QUnit.test('test options exist and default values', function( assert ) {

    // Stub the winow alert method using sinon.
    var alertStub = sinon.stub(window, "alert", function(msg) { return true; } );
    $('#target').formdialog();

    // Assert a dialog window opened, caused by the lack of parameters passed
    sinon.assert.called(window.alert);

    // Grab the jQuery plugin data assigned to the DOM element.
    var options = $('#target').data('gten-formdialog').options;

【问题讨论】:

    标签: javascript gruntjs qunit sinon


    【解决方案1】:

    如果我没记错的话,你需要从你的存根中return true;(或假)......我想。至少,这就是我一直看到的方式,以及 various 其他 SO answers 的方式。所以试试这个:

    QUnit.test('test options exist and default values', function( assert ) {
    
    // Stub the winow alert method using sinon.
    var alert = sinon.stub(window, "alert", function(msg) { return true; } );
    $('#target').formdialog();
    
    // Assert a dialog window opened, caused by the lack of parameters passed
    sinon.assert.called(window.alert);
    
    // Grab the jQuery plugin data assigned to the DOM element.
    var options = $('#target').data('gten-formdialog').options;
    

    【讨论】:

    • 感谢您的反馈,非常感谢。不幸的是没有答案 :-( 仍然遇到同样的问题,但我修改了上面的代码以反映我最近的尝试。
    • 当...尝试自己模拟assert方法以确认问题出在Sinon怎么样?您可以这样做:window.alert = function() { return true; }; 注意:如果您尝试这样做,您的断言将不会真正起作用,但这会告诉您 Phantom 是否超时。
    猜你喜欢
    • 2015-11-28
    • 2015-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 2014-11-29
    • 2020-12-12
    • 1970-01-01
    相关资源
    最近更新 更多