【发布时间】:2013-12-16 17:25:32
【问题描述】:
我的 jasmine 中有某些案例的测试场景.. 但我无法获得这三个场景的测试用例...... 你能告诉我怎么做吗...
- 应该返回的数字是数组中的第一个或最后一个 数组中未定义的值(或 NaN、Infinity、...)\ 数字与 绝对值相同(例如 -0.01 和 0.01)
也提供我的小提琴
var myNumbersToSort = [-1, 2, -3, 4, 0.3,1,-0.001];
function getClosestToZero(set) {
if(0 === set.length) return null;
var closest = Math.abs(set[0]), result = 0;
for(var i in set) {
var next = Math.abs(set[i]);
if(closest > next) {
result = i;
closest = next;
}
}
return closest;
}
document.getElementById('output').innerHTML = (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 );
} );
} );
【问题讨论】:
-
查看代码,我想您的测试没有通过第二个测试,但应该通过第三个测试。这是因为您比较并返回值的绝对值。所以第二个测试将返回 0.01 而不是 -0.01
-
@cbayram:感谢您的回复..您能编辑我的小提琴以及如何通过第二次测试jsfiddle.net/YYg8U/15
标签: javascript jquery unit-testing testing jasmine