Ad

How Come My Unit Testing Only Passes Negative Cases?

- 1 answer

I am trying to set up a unit test for a little practice I am doing in JS.

I am using Jasmine unit test framework for this.

However, when running the unit tests on my little JS application (a prime number machine) it only passes the cases where it expects the number NOT to be prime.

and it fails all the prime number cases.

I cannot seem to figure out why, since my application in the actual HTML I embedded this in, works just fine.

My application looks like this.

function findPrime(){
            //get the input value
            var num = 0;
            var c = 0;

            //loop till i equals to $num
            for (i = 1; i <= num; i++) {
                //check if the $num is divisible by itself and 1
                // % modules will give the reminder value, so if the reminder is 0 then it is divisible
                if (num % i == 0) {
                    //increment the value of c
                    c = c + 1;
                }
            }

            //if the value of c is 2 then it is a prime number
            //because a prime number should be exactly divisible by 2 times only (itself and 1)
            if (c == 2) {
                return true;
            }else{
                return false;
            }
        }

And my unit test looks like this.

    describe("PrimeNumberMachineTest", function() {
  it("should determine 2 is a prime number", function() {
    expect(findPrime(2)).toBeTruthy();
  });
  it("should determine 3 is a prime number", function() {
    expect(findPrime(3)).toBeTruthy();
  });
  it("should determine 4 is not a prime number", function() {
    expect(findPrime(4)).toBeFalsy();
  });
  it("should determine 5 is a prime number", function() {
    expect(findPrime(5)).toBeTruthy();
  });
  it("should determine 6 is not a prime number", function() {
    expect(findPrime(6)).toBeFalsy();
  });
  it("should determine 7 is a prime number", function() {
    expect(findPrime(7)).toBeTruthy();
  });
  it("should determine 8 is not a prime number", function() {
    expect(findPrime(8)).toBeFalsy();
  });
  it("should determine 9 is not a prime number", function() {
    expect(findPrime(9)).toBeFalsy();
  });
  it("should determine 10 is not a prime number", function() {
    expect(findPrime(10)).toBeFalsy();
  });
  it("should determine 11 is a prime number", function() {
    expect(findPrime(11)).toBeTruthy();
  });
  it("should determine 12 is not a prime number", function() {
    expect(findPrime(12)).toBeFalsy();
  });
  it("should determine 13 is a prime number", function() {
    expect(findPrime(13)).toBeTruthy();
  });
  it("should determine 14 is not a prime number", function() {
    expect(findPrime(14)).toBeFalsy();
  });
  it("should determine 15 is not a prime number", function() {
    expect(findPrime(15)).toBeFalsy();
  });
  it("should determine 16 is not a prime number", function() {
    expect(findPrime(16)).toBeFalsy();
  });
  it("should determine 17 is a prime number", function() {
    expect(findPrime(17)).toBeTruthy();
  });
  it("should determine 18 is not a prime number", function() {
    expect(findPrime(18)).toBeFalsy();
  });
  it("should determine 19 is a prime number", function() {
    expect(findPrime(19)).toBeTruthy();
  });
  it("should determine 20 is not a prime number", function() {
    expect(findPrime(20)).toBeFalsy();
  });
  it("should determine 37,120,123 is a prime number", function() {
    expect(findPrime(37120123)).toBeTruthy();
  });
});

But the results come back as follows:

Jasmine Results

Ad

Answer

Quite simply, because your function always returns false.

The function doesn't take any parameters and doesn't depend on any external variables, so it's impossible for it to ever return anything but one value.

If you change it to function findPrime(num) and remove the var num line it should work a little better.

Ad
source: stackoverflow.com
Ad