Tolvic
Tolvic

Reputation: 145

Test cases in Jasmine. Is it possible to run the same test on multiple different data sets?

I would like to run a Jasmine tests multiple times using different test data and when any test fails that specific test case should be easily identifiable.

In the NUnit testing framework this is can achievable with the TestCase attribute.

I have tried putting the it block inside of a foreach block and while the tests show, they do not run, as can be seen here:

enter image description here

Below is the implementation I have tried:

/// <reference path="../Jasmine/jasmine.js"/>
/// <reference path="../Jasmine/jasmine-html.js"/>
/// <reference path="../../Site/wwwroot/lib/jquery/dist/jquery.js"/>
/// <reference path="../../Site/wwwroot/js/nlHoldem.js"/>

describe("nlHoldem.js", function () {
    var mockHtml;

    var deckOfCards = [
        { id: 'ace-of-spades' },
        { id: 'king-of-spades' }
    ];

    beforeEach(function () {
        mockHtml = getMockHtml();
        $(document.body).append(mockHtml);
        nlHoldem.init();
    });

    afterEach(function () {
        $('#mock-html-container').remove();
        sessionStorage.clear();
    });

    deckOfCards.forEach(function (card) {
        it("should add styling of top -5px to "+ card.id +" on mouseover", function () {
            // Arrange
            var targetCard = $("#" + card.id);

            // Act
            targetCard.mouseover();


            // Assert
            expect(targetCard.css("top")).toBe("-5px");
        });
    });
});

Upvotes: 2

Views: 2055

Answers (1)

aimeesophia
aimeesophia

Reputation: 36

If you want to show which specific test case failed in the for loop (inside of the it block as @ruby_newbie suggested), you can add a custom failure message like so:

it("should add styling of top -5px to card on mouseover", function () {
    deckOfCards.forEach(function(card) {
        // Arrange
        var targetCard = $("#" + card.id);

        // Act
        targetCard.mouseover();


        // Assert
        expect(targetCard.css("top")).toBe("-5px", "failed on: " + card.id);
    });
});

Upvotes: 2

Related Questions