BrianRT
BrianRT

Reputation: 1962

JavaScript casting number to string test with Jasmine

I'm writing some unit tests for my JS code using the Jasmine framework to learn the basics of Jasmine. This is more of a JS question than it is a Jasmine question.

I checked out the JS string conversion methods and looked at Casting to string in JavaScript. I may be using toString() incorrectly.

The function I wrote looks like:

function factorial(input) {

    var result = 1;

    if(input === 0) {
        result = 0;
    } else if(input < 0) {
        result = "cannot compute factorial of negative number";
    } else if(input.isString) {
        result.toString();
        result = "input is not a number";
    } else {
        for(var i = 1; i <= input; i++) {
            result *= i;
        }
    }
    return result;
}

The Jasmine spec looks like:

describe("Factorializer", function() {
    it("factorial() should return the correct factorial value of an input > 
        0.", function() {
            expect.factorial(3)).toBe(6);
    });

    it("factorial() should return 0 if the input = 0.", function() {
        expect.factorial(0)).toBe(0);
    });

    it("factorial() should return 1 if the input = 1.", function() {
        expect.factorial(1)).toBe(1);
    });

    it("factorial() should return an error if the input is negative.",
        function() {
            expect.factorial(-5)).toBe("cannot computer factorial 
                negative number");
    });

    it("factorial() should return an error if the input is not a 
        number.", function() {
            expect.factorial("Herro")).toBe("input is not a number");
    });

    it("factorial() should return an error if the input is not a 
        number.", function() {
            expect.factorial("Herro")).toBeNaN();
    });
});

Whenever the input is a string, result is always 1. Either the else if(input.isString) is never entered, or the result is not being assigned the value it's supposed to in the statement. I am leaning towards the former, because the previous else if seems to be working (it passes the Jasmine test). The last two Jasmine tests are failing, which is good since it confirms that Jasmine is working and I caught a problem with my code.

I'm trying the fix the problem that Jasmine found; I'm not trying to alter the Jasmine tests unless their is something wrong with them, which I don't think there is.

Upvotes: 0

Views: 2343

Answers (2)

Randy Hunt
Randy Hunt

Reputation: 425

} else if(input.isString) {
    result.toString();
    result = "input is not a number";

The above isn't doing anything.

There is no isString property in javascript, and .toString() is a function that returns a value, it does not edit in place, and it wouldn't matter anyway because you're overwriting it on the following line anyway!

Personally, I'd go with something more like

input = parseInt(number, 10);
if (isNaN(input)) {
    result = 'input is not a number';
}

Upvotes: 0

Sean
Sean

Reputation: 4515

Replace input.isString with typeof input === 'string', and get rid of result.toString();. It's not doing anything useful.

Edit: Also, your last two tests appear to be redundant.

Upvotes: 2

Related Questions