Javascript 针对每个场景使用Jasmine的测试用例
我正试图为我的分类程序编写一个jasmine单元测试。。 我在写jasmine和测试用例方面是新手。。 在下面提供我的代码。。。 你们能告诉我怎么用小提琴演奏吗Javascript 针对每个场景使用Jasmine的测试用例,javascript,unit-testing,jasmine,testcase,jasmine-jquery,Javascript,Unit Testing,Jasmine,Testcase,Jasmine Jquery,我正试图为我的分类程序编写一个jasmine单元测试。。 我在写jasmine和测试用例方面是新手。。 在下面提供我的代码。。。 你们能告诉我怎么用小提琴演奏吗 var myNumbersToSort=[-1,2,3,4,0.3,0.001]; 函数getClosestToZero(numberSet){ VarI=0,positiveSet=[],positiveClosest=0; 对于(i=0;i=0?numberSet[i]:numberSet[i]*-1); } positiveCl
var myNumbersToSort=[-1,2,3,4,0.3,0.001];
函数getClosestToZero(numberSet){
VarI=0,positiveSet=[],positiveClosest=0;
对于(i=0;i=0?numberSet[i]:numberSet[i]*-1);
}
positiveClosest=Math.min.apply(Math,positiveSet);
返回编号集合[正集合索引(正集合关闭)];
}
警报(getClosestToZero(myNumbersToSort));
示例测试用例可能如下所示
describe( 'getClosestToZero', function () {
it( 'finds a positive unique number near zero', function () {
expect( getClosestToZero( [-1, 0.5, 0.01, 3, -0.2] ) ).toBe( 0.01 );
} );
it( 'finds a negative unique number near zero', function () {
expect( getClosestToZero( [-1, 0.5, -0.01, 3, -0.2] ) ).toBe( -0.01 );
} );
// your method actually doesn't pass this test
// think about what your method *should* do here and if needed, fix it
it( 'finds one of multiple identical numbers near zero', function () {
expect( getClosestToZero( [-1, 0.5, 0.01, 0.01, 3, -0.2] ) ).toBe( 0.01 );
} );
} );
您可以考虑更多的测试用例。测试任何积极和消极的行为,并尝试思考边缘案例。请记住,测试不仅是为了证明您的代码当前正在工作,而且是为了确保它在未来的开发过程中不会中断
可能的边缘情况:
- 应该返回的数字是数组中的第一个或最后一个
数组中未定义的值(或
,NaN
,…)无穷大
- 绝对值相同的数字(例如
和-0.01
)0.01
- 示例测试用例可能如下所示
describe( 'getClosestToZero', function () {
it( 'finds a positive unique number near zero', function () {
expect( getClosestToZero( [-1, 0.5, 0.01, 3, -0.2] ) ).toBe( 0.01 );
} );
it( 'finds a negative unique number near zero', function () {
expect( getClosestToZero( [-1, 0.5, -0.01, 3, -0.2] ) ).toBe( -0.01 );
} );
// your method actually doesn't pass this test
// think about what your method *should* do here and if needed, fix it
it( 'finds one of multiple identical numbers near zero', function () {
expect( getClosestToZero( [-1, 0.5, 0.01, 0.01, 3, -0.2] ) ).toBe( 0.01 );
} );
} );
您可以考虑更多的测试用例。测试任何积极和消极的行为,并尝试思考边缘案例。请记住,测试不仅是为了证明您的代码当前正在工作,而且是为了确保它在未来的开发过程中不会中断
可能的边缘情况:
- 应该返回的数字是数组中的第一个或最后一个
数组中未定义的值(或
,NaN
,…)无穷大
- 绝对值相同的数字(例如
和-0.01
)0.01
函数getClosestToZero(numberSet){var current,i=0,l=numberSet.length,min=Number.POSITIVE(无穷大),min(无穷大)函数更有效的版本是:函数getClosestToZero(numberSet){var current,i=0,l=numberSet.length,min=Number.POSITIVE_∞,min_abs=Number.POSITIVE_∞;因为如果你知道茉莉花是什么,你就不必问这个。如果你知道茉莉花是什么,你就不必问这个。