javascript - QUnit : One test per method with multiple assertions or multiple tests per method?

标签 javascript unit-testing qunit

我已决定为我的下一个 javascript 项目开始使用 TDD,并且我正在使用 QUnit 进行单元测试。我对单元测试完全陌生,从未用任何语言做过。下面是我的模块之一的示例,外加一个针对 find 方法的测试,该方法试图涵盖该方法将遇到的所有场景:

module("TextSwapper", {
    setup: function() { 
        this.str = 'When you first start off trying to solve a problem, the first solutions you come up with are very complex, and most people stop there. But if you keep going, and live with the problem and peel more layers of the onion off, you can often times arrive at some very elegant and simple solutions.';

        this.ts = new TextSwapper();
        ok(this.ts, 'The TextSwapper was created successfully');

        this.textarea = document.createElement('textarea');
        this.textarea.value = this.str;
        document.body.appendChild(this.textarea);
    },
    teardown: function() {
            document.body.removeChild(this.textarea);
    }
});

test("find()", function() {
    equal(this.ts.find('When'), false, "Check it fails gracefully when no input has been set.");
    this.textarea.focus();
    equal(this.ts.find('When'), true, "Check it finds 'When' if the textarea has focus but no input has been set.");

    this.ts.setInput(this.textarea);
    this.ts.find('When');
    equal(window.getSelection().toString(), 'When', 'The word \'When\' should be highlighted');
    equal(this.ts.found[0][0], 'When', 'The word \'When\' should be in the found array');

    this.ts.find('you');
    equal(window.getSelection().toString(), 'you', 'The word \'you\' should be highlighted');
    equal(this.ts.found[1][0], 'you', 'The word \'you\' should be in the found array');
    equal(this.ts.found.length, 4 ,'Should have found 4 you\'s');

    this.ts.find('bill');
    equal(this.ts.found.length, 0, 'Check that nothing was found for \'bill\'');

    this.ts.find('[a-z]*ee[a-z]*');
    equal(window.getSelection().toString(), 'keep', 'The word \'keep\' should be highlighted');
    equal(this.ts.found[1][0], 'peel', 'The word \'peel\' should be in the found array');
    equal(this.ts.found.length, 2 ,'Should have found 2 for [a-z]*ee[a-z]*');

});

我的问题是我这样做的方式是否正确?我的测试中是否有太多断言?我的测试是否应该分解成更小的测试?我一直在 stackoverflow 上阅读关于 TDD 的文章,现在我读了一些让我觉得我做错了的东西。

最佳答案

如果您使用的是 TDD,那么您应该为每种测试方法制定一个断言。

以下链接很好地解释了在使用一种方法测试所有内容时可能遇到的问题:Unit testing它可以更容易地在您的代码中引入隐藏的错误。

关于javascript - QUnit : One test per method with multiple assertions or multiple tests per method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7822876/

相关文章:

javascript - Angular JS, Protractor 定位器,获取元素的直接子元素

javascript - dc.js 和 crossfilter - 根本不读取 json

javascript - MySQL 捆绑在 NPM 应用程序中

javascript - 对数复杂度 : Either the book has a typo or what's happening here?

.net - VB.NET中的问题单元测试

unit-testing - 使用 sinon 断言使用所需参数进行了特定的 stub 调用

javascript - 如何在 Jest 中进行单元测试并检查函数是否正在调用预期的 firebase 方法?

python - Pytest - 模拟模拟的嵌套属性函数/方法的副作用

javascript - QUnit Javascript 单元测试显示断言号而不是测试号

javascript - 为什么 SauceLabs 在我的 QUnit 测试明显通过时却说它失败了?