Jasmine Example Specification

Is there a way to implement examples (or tables) in your specification when using Jasmine?

I really like the syntax of Jasmine, but I can define examples that I find much more important.

I want to pass the following 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  |
+4
source share
1 answer

You can use jasmine as follows:

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]);
        }
    );
});
0
source

All Articles