我正在开发一个项目,我必须使用Jasmine测试我的js代码。我写了这样的测试。
describe("Function: foo() --- File: foo.js", function() {
it("if input is integer", function () {
var result = foo(123);
expect(result).toBe(123);
});
it("If input is string", function () {
var result = foo('String');
expect(result).toBe('String');
});
it("If input is empty string", function () {
var result = foo('');
expect(result).toBe('');
});
});
正如代码所示,我正在重复同样的事情'它'一次又一次阻止。只有正在改变的是描述,输入功能和输出" toBe"。我想询问是否有一种方法可以在list / dictionary中添加我的方案,并在我的测试中使用它们而不使用任何for循环
describe("Function: foo() --- File: foo.js", function() {
scenarios = [
{description: "if input is integer", input: 123, output: 123},
{description: "if input is String", input: "String", output: "String"},
{description: "if input is empty string", input: '', output: ''},
];
//This will run for each scenario in above list
it(this.description, function () {
var result = foo(this.input);
expect(result).toBe(this.output);
});
});
在这种情况下,任何人都可以帮助我吗?
答案 0 :(得分:2)
您可以使用using
:
describe("Function: foo() --- File: foo.js", function() {
var scenarios = [
{description: "if input is integer", input: 123, output: 123},
{description: "if input is String", input: "String", output: "String"},
{description: "if input is empty string", input: '', output: ''},
];
scenarios.map(function(value) {
it(value.description, function() {
var result = foo(value.input);
expect(result).toBe(value.output);
});
});
}