使用Jasmine按实例说明

时间:2015-01-19 04:53:03

标签: javascript cucumber jasmine karma-jasmine gherkin

使用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  |

1 个答案:

答案 0 :(得分:0)

你可以像这样使用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]);
        }
    );
});
相关问题