【发布时间】:2012-11-03 21:08:38
【问题描述】:
public Double Invert(Double? id)
{
return (Double)(id / id);
}
我已经为这个测试做了这个但是失败了请任何人帮助这个因为刚开始单元测试
/* HINT: Remember that you are passing Invert an *integer* so
* the value of 1 / input is calculated using integer arithmetic.
* */
//Arrange
var controller = new UrlParameterController();
int input = 7;
Double expected = 0.143d;
Double marginOfError = 0.001d;
//Act
var result = controller.Invert(input);
//Assert
Assert.AreEqual(expected, result, marginOfError);
/* NOTE This time we use a different Assert.AreEqual() method, which
* checks whether or not two Double values are within a specified
* distance of one another. This is a good way to deal with rounding
* errors from floating point arithmetic. Without the marginOfError
* parameter the assertion fails.
* */
【问题讨论】:
-
我没有看到问题,请在您的帖子中更具体。
-
这似乎更多地是关于浮点运算而不是单元测试。
Assert的结果是什么? -
你为什么使用可以为空的 Double?无论如何,除以 null 是不可能的,所以它只会导致一个神秘的错误。
-
@aulme: 除以 null 不是不可能,它只是 null msdn.microsoft.com/en-us/library/2cf62fcy(v=vs.80).aspx "预定义的一元和二元运算符以及为值类型存在的任何用户定义的运算符可能也可用于可空类型。如果操作数为空,这些运算符会产生空值;否则,运算符将使用包含的值来计算结果。但是,当然,转换回不可为空的类型会失败。
-
@JayC -“System.InvalidOperationException:可空对象必须有一个值。”你不能在这里使用null作为id并尝试划分。
标签: c# .net asp.net-mvc-3 unit-testing controller