Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 针对每个场景使用Jasmine的测试用例_Javascript_Unit Testing_Jasmine_Testcase_Jasmine Jquery - Fatal编程技术网

Javascript 针对每个场景使用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

我正试图为我的分类程序编写一个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);
}
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_∞;因为如果你知道茉莉花是什么,你就不必问这个。如果你知道茉莉花是什么,你就不必问这个。