他们是否可以在一次测试中使用多个参数,而不是再次复制和粘贴该功能?
NUnit for C#中的示例:
[TestCase("0", 1)]
[TestCase("1", 1)]
[TestCase("2", 1)]
public void UnitTestName(string input, int expected)
{
//Arrange
//Act
//Assert
}
我想要的Js:
describe("<Foo />", () => {
[TestCase("false")]
[TestCase("true")]
it("option: enableRemoveControls renders remove controls", (enableRemoveControls) => {
mockFoo.enableRemoveControls = enableRemoveControls;
//Assert that the option has rendered or not rendered the html
});
});
答案 0 :(得分:4)
您可以将it
- 调用放在函数中并使用不同的参数调用它:
describe("<Foo />", () => {
function run(enableRemoveControls){
it("option: enableRemoveControls renders remove controls", () => {
mockFoo.enableRemoveControls = enableRemoveControls;
//Assert that the option has rendered or not rendered the html
});
}
run(false);
run(true);
});
答案 1 :(得分:4)
另一种方法是使用Jest。它有functionality内置:
test.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('returns $expected when $a is added $b', ({a, b, expected}) => {
expect(a + b).toBe(expected);
});
答案 2 :(得分:0)
如果您使用的是Mocha,可以将其与mocha-testdata结合使用:
import * as assert from assert;
import { given } from mocha-testdata;
describe('<Foo />', function () {
given([
{ input: true, expected: 'some expected value', description: 'flag enabled' },
{ input: false, expected: 'some other expected value', description: 'flag disabled' },
]).
it('option: enableRemoveControls renders remove controls', function ({ input, expected }) {
// prepare, act, assert
});
});
在上面的示例中,您还会注意到一个description
字段没有注入到测试中。
这个小技巧可以使reported test name更有意义。
希望这会有所帮助!
Jan