mercredi 23 décembre 2015

How can I write Scenarios in Jasmine.js

I am working on a project in which I have to test my js code with Jasmine. I wrote my tests like this.

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('');
    });

});

As code shows, I am repeating same 'it' block again and again. Only thing which is changing is description, input to function and output in "toBe". I want to ask if there is a way with which I can add my scenarios in list/dictionary and use them in my tests like this without using any for loop

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);
    });

});

Can any one help me in this case?

Aucun commentaire:

Enregistrer un commentaire