使用函数参数进行Javascript单元测试

时间:2014-03-12 05:58:07

标签: javascript jquery

function calculatePrice(base_cost, base_dist, additional_cost, additional_dist, total_dist) {

  if (total_dist > base_dist) {



  } else {
     return base_cost;
  }


}

function unitTest() {
  var cost;

  cost = calculatePrice(10,20,5,5,15);
  if (cost != 10) console.log ("FAILED 1");

  cost = calculatePrice(10,10,5,5,15);
  if (cost != 15) console.log ("FAILED 2");

  cost = calculatePrice(20,10,5,1,15);
  if (cost != 45) console.log ("FAILED 3");

  cost = calculatePrice(20,10,5,1,1);
  if (cost != 20) console.log ("FAILED 4");

  cost = calculatePrice(10,10,5,1,14);
  if (cost != 30) console.log ("FAILED 5");

  cost = calculatePrice(10,10,5,2,14);
  if (cost != 20) console.log ("FAILED 6");

  console.log('Test complete');

}

我想计算unitTest的函数值来传递。我只传递1并需要在calculatePrice函数中写一些密码。 非常感谢

1 个答案:

答案 0 :(得分:2)

您需要使用Math.ceil

计算将是那样的

var dist_diff = total_dist - base_dist;
var time = Math.ceil(dist_diff / additional_dist);
return (time * additional_cost) + base_cost;

因此,您将获得测试完整结果且没有错误。