0
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.
 * */  
4

2 回答 2

2

It seems you want to test your controller to "invert" a value. It probably would help if you weren't dividing a value by itself.

The only things that can happen are:

  1. you get a result of "1" (hint, hint)
  2. you get "NaN" ( 0/0 )
  3. you get an cast error by passing in null.
于 2012-11-03T22:59:06.573 回答
0

通过您的代码示例,我认为您正在寻找的是控制器中的逆方法。

public Double Invert(Double? id)
{
    //replace id with 1 -- (1/id) gives you an inverse of id.  
    return (Double)(1 / id);
}
于 2012-11-04T06:54:17.317 回答