gpt4 book ai didi

javascript - 使用 Jasmine 的实例规范

转载 作者:搜寻专家 更新时间:2023-11-01 04:41:12 26 4
gpt4 key购买 nike

使用 Jasmine 时,有没有办法在您的规范中实现示例(或表格)?

我真的很喜欢 Jasmine 语法,但我发现能够定义示例更为重要。

我希望将以下内容移植到 Jasmine:

Scenario Outline: eating
Given there are <start> cucumbers
When I eat <eat> cucumbers
Then I should have <left> cucumbers

Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |

最佳答案

你可以像这样使用 Jasmine:

describe("Eating - Table like Tests", function () {

// the test cases
var testCasesTable = [[12, 5, 7], [20, 5, 15], [7, 3, 4]];

// the tested function - should be in a seperate file
var eating = function (start, eat) {
return start - eat;
};

// the test function
var eatingTest = function (start, eat, left) {
it('Given there are ' + start + ' cucumbers, When I eat ' + eat + ' cucumbers, Then I should have ' + left + ' cucumbers', function () {
expect(eating(start, eat)).toBe(left);
});
};

// the loop function that goes over the test cases and run them
testCasesTable.forEach(function (testCase) {
eatingTest(testCase[0], testCase[1], testCase[2]);
}
);
});

关于javascript - 使用 Jasmine 的实例规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28018146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com