Praveen
Praveen

Reputation: 347

How to write unit test for switch case using jasmine

below is my code

var s=function(){
return 'API CALL';//Through some API call i am getting and integer value
};

switch(s()){
case 0: store.p1="Some Text"; break;
case 1: store.p1="Some Other Text"; break;
case 2: store.p1="Another Text"; break;
}

In the above function i am firing API call,which returns an integer value(0,1,2) and storing it in 's', i wanted to write unit test using jasmine..., could anyone please help how to write test case?!

Note: 'store' is global variable and p1 is a another variable in 'store' where i want to store the value of 's'.

Upvotes: 1

Views: 13378

Answers (4)

DevCix
DevCix

Reputation: 1

In simplest terms you don't test a switch.

A switch, just like if, else, where, when and other such things are language construct, so needs no testing. Testing a portion of your code that deals with flow control, you write different test for each outcome of the flow.

This means a separate suite for each option.

function rockPaperScisors(){ ... // switch statement for the return value }

describe('rock paper scissors caller', function(){
    beforeEach(function(){
        var throw;
    });

    it("should return rock when 1 is entered", function(){
        throw = 1;
        expect(throw).toEqual('rock');
    });

    it("should return paper when 2 is entered", function(){
        throw = 2;
        expect(throw).toEqual('paper');
    });

    it("should return scissor when 3 is entered", function(){
        throw = 3;
        expect(throw).toEqual('scisor');
    });
});

Upvotes: 0

inf3rno
inf3rno

Reputation: 26137

Most of the cases (just like in yours) using map is a better choice than using switch.

function f(){
    var myInteger = syncApiCallReturnsInteger();
    var map = {
        0: "Some Text",
        1: "Some Other Text",
        2: "Another Text"
    };
    store.p1 = map[myInteger];
}

If you want to test this, you have to mock out the syncApiCallReturnsInteger(). In order to do that it must be injectable. The store must be injectable as well, so you can check it's changes.

function f(syncApiCallReturnsInteger, store){
    var myInteger = syncApiCallReturnsInteger();
    var map = {
        0: "Some Text",
        1: "Some Other Text",
        2: "Another Text"
    };
    store.p1 = map[myInteger];
}

The test:

describe("blah", function() {
    it("f sets store.p1", function() {
        var fixture2expectation = {
            0: "Some Text",
            1: "Some Other Text",
            2: "Another Text"
        };

        for (var fixtureString in fixture2expectation){
            var fixture = parseInt(fixtureString);
            var expectation = fixture2expectation[fixture];
            var mockApiCall = function (){return fixture;};
            var mockStore = {};
            f(mockApiCall, mockStore);
            expect(mockStore.p1).toBe(expectation);
        }
    });
});

If you don't inject the dependencies properly, then the only thing you can do is using and overriding globals, which is generally a bad idea, because there will be variable name collisions later when your project grows.

Upvotes: 0

ktharsis
ktharsis

Reputation: 3190

To correctly test your API function you will need to mock it out and have it fake the return value. There are many ways to do this, and some of it will depend on how your API works (angular http, service, promise, etc).

Take a look at jasmine spys. They will probably be the easiest if it will work for your situation. Something along the lines of:

spyOn(APIObject, "APIMethod").and.returnValue(1);  
expect(store.p1).toBe(value 1 stored);

If you don't have an mock object to spyOn you can create one using createSpy and it will work the same way.

Upvotes: 0

Abdul Ahmad
Abdul Ahmad

Reputation: 10021

why don't you make this switch statement a function that takes a parameter and returns a message based on the parameter. This way the unit test will be easier because you can unit test the function.

function getValueStoredMessage(integer) {
 return 'value ' + integer + ' stored';
}

and you can call it like this:

store.p1 = getValueStoredMessage(valueFromApi);

I personally haven't written many unit tests, but I imagine a function would be easier than a switch?

Upvotes: 0

Related Questions