Я пытаюсь настроить юнит-тест для небольшой практики, которую я делаю в JS.
Для этого я использую фреймворк для модульных тестов Jasmine.
Однако при запуске модульных тестов на моем маленьком JS-приложении (машине с простыми числами) оно проходит только в тех случаях, когда ожидает, что число НЕ будет простым.
и он не работает во всех простых числах.
Я не могу понять, почему, так как мое приложение в реальном HTML, в которое я его встроил, работает просто отлично.
Мое приложение выглядит так.
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;
}
}
И мой юнит-тест выглядит следующим образом.
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();
});
});
Но результаты возвращаются следующим образом:
![Jasmine Results](https://i.stack.imgur.com/rH44K.jpg)