Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在单元测试中使用Sinon更改属性值?_Jquery_Unit Testing_Sinon_Qunit - Fatal编程技术网

Jquery 如何在单元测试中使用Sinon更改属性值?

Jquery 如何在单元测试中使用Sinon更改属性值?,jquery,unit-testing,sinon,qunit,Jquery,Unit Testing,Sinon,Qunit,我有以下代码: AlertHelper = function () { return { DisplayToastMessage: function() { if ($("body > .modal.small").length > 1) { AlertHelper.CalculateAdditionalHeight(); } } } }(); 我试着用Q

我有以下代码:

AlertHelper = function () {
    return {
        DisplayToastMessage: function() {
            if ($("body > .modal.small").length > 1) {
                AlertHelper.CalculateAdditionalHeight();
            }
        }
    }
}();
我试着用QUnit和SinonJS编写单元测试

这是单元测试:

QUnit.test('DisplayToastMessage - testing function call', function (assert) {
    var sandbox = sinon.sandbox.create();
    var mock = sinon.mock(AlertHelper);

    sandbox.stub($("body > .modal.small"), "length", 42);

    var expectationCalculateAdditionalHeight = mock.expects("CalculateAdditionalHeight");
    expectationCalculateAdditionalHeight.once();

    AlertHelper.DisplayToastMessage();

    mock.verify();
    assert.ok(mock.verify(), "CalculateAdditionalHeight function is called once");

    sandbox.restore();
    mock.restore();
});
但我收到一个错误“无法读取未定义的属性‘length’”。
如何测试调用了CalculateAdditionalHeight函数?

尝试模拟实例:

$=sinon.stub();

$.withArgs('body>.modal.small')。返回({“length”:42})

经过研究,我找到了解决方案:

QUnit.test('DisplayToastMessage - testing function call', function (assert) {
    var mock = sinon.mock(AlertHelper);
    var fake$ = sinon.stub(window, '$');

    fake$.withArgs('body > .modal.small').returns({ "length": 42 });

    var expectationCalculateAdditionalHeight = mock.expects("CalculateAdditionalHeight");
    expectationCalculateAdditionalHeight.once();

    AlertHelper.DisplayToastMessage();

    mock.verify();
    assert.ok(mock.verify(), "CalculateAdditionalHeight function is called once");

    fake$.restore();
    mock.restore();
});

我想我需要在末尾添加$.restore();我说得对吗?这很有效,但我需要恢复价值。您知道如何恢复存根吗?是的,您可以使用$.restore()进行恢复;请参阅上的文档。此外,您还可以使用beforeach/before为每个测试用例设置期望值;。出现错误“$。还原不是函数”。你知道为什么吗?我发现在覆盖
$
之后,无法恢复它,因为引用没有创建。