【发布时间】:2017-10-29 23:08:43
【问题描述】:
我有这个代码:
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();
});
但我收到错误“无法读取未定义的属性‘长度’”。 如何测试调用了CalculateAdditionalHeight 函数?
【问题讨论】:
标签: jquery unit-testing sinon qunit