Javascript 为什么我的单元测试只通过了否定的情况?
我正在尝试为我在JS中所做的一个小练习设置一个单元测试 为此,我使用Jasmine单元测试框架 然而,当在我的小JS应用程序(质数机器)上运行单元测试时,它只会通过它希望数字不是质数的情况 它失败了所有素数的情况 我似乎不明白为什么,因为我的应用程序在我嵌入它的实际HTML中工作得很好 我的应用程序看起来像这样Javascript 为什么我的单元测试只通过了否定的情况?,javascript,unit-testing,jasmine,Javascript,Unit Testing,Jasmine,我正在尝试为我在JS中所做的一个小练习设置一个单元测试 为此,我使用Jasmine单元测试框架 然而,当在我的小JS应用程序(质数机器)上运行单元测试时,它只会通过它希望数字不是质数的情况 它失败了所有素数的情况 我似乎不明白为什么,因为我的应用程序在我嵌入它的实际HTML中工作得很好 我的应用程序看起来像这样 function findPrime(){ //get the input value var num = 0;
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();
});
});
但结果如下:
非常简单,因为函数总是返回false 该函数不接受任何参数,也不依赖任何外部变量,因此它不可能只返回一个值
如果您将其更改为
函数findprome(num)
并删除var num
行,它应该会工作得更好 非常简单,因为函数总是返回false
该函数不接受任何参数,也不依赖任何外部变量,因此它不可能只返回一个值
如果您将其更改为
函数findprome(num)
并删除var num
行,它应该会工作得更好 我认为唯一的问题是您的函数findprome()
不接受任何参数。您将
num
变量值设置为0
,因此始终测试0
的素数(?) 我认为唯一的问题是您的函数findprome()
不接受任何参数。您将
num
变量值设置为0
,因此始终测试0
的素数(?) 谢谢,我真不敢相信我犯了这样的错误,这有助于解决问题。再次感谢!谢谢,我真不敢相信我犯了这样的错误,这有助于解决问题。再次感谢!旁注:您不需要循环到数字,但只要检查到它的平方根就足够了。如果要返回布尔比较语句,只需返回它即可。不要做if(cond){true}或者{false}
,而是做:返回cond
,在你的例子中返回c==2代码>。我在测试用例中看不到任何问题,因此可能是函数中存在一些错误。检查它。旁注:你不需要循环到这个数字,但它足以检查到它的平方根。如果要返回布尔比较语句,只需返回它即可。不要做if(cond){true}或者{false}
,而是做:返回cond
,在你的例子中返回c==2代码>。我在测试用例中看不到任何问题,因此可能是函数中存在一些错误。检查一下。