代码改变世界

[翻译]NUnit---Equality Asserts&& Identity Asserts (四)

2013-03-31 09:58  Max蚊子  阅读(985)  评论(0编辑  收藏  举报

 

Equality Asserts

  这些方法测试两个参数是否相等。语言不自动装修的普通类型可以使用对应的重载的方法。 

View Code
Assert.AreEqual( int expected, int actual );
Assert.AreEqual( int expected, int actual, string message );
Assert.AreEqual( int expected, int actual, string message, 
                 params object[] parms );
                 
Assert.AreEqual( uint expected, uint actual );
Assert.AreEqual( uint expected, uint actual, string message );
Assert.AreEqual( uint expected, uint actual, string message, 
                 params object[] parms );

Assert.AreEqual( decimal expected, decimal actual );
Assert.AreEqual( decimal expected, decimal actual, string message );
Assert.AreEqual( decimal expected, decimal actual, string message, 
                 params object[] parms );

Assert.AreEqual( float expected, float actual, float tolerance );
Assert.AreEqual( float expected, float actual, float tolerance,
                 string message );
Assert.AreEqual( float expected, float actual, float tolerance,
                 string message, params object[] parms );

Assert.AreEqual( double expected, double actual, double tolerance );
Assert.AreEqual( double expected, double actual, double tolerance,
                 string message );
Assert.AreEqual( double expected, double actual, double tolerance,
                 string message, params object[] parms );

Assert.AreEqual( object expected, object actual );
Assert.AreEqual( object expected, object actual, string message );
Assert.AreEqual( object expected, object actual, string message, 
                 params object[] parms );

Assert.AreNotEqual( int expected, int actual );
Assert.AreNotEqual( int expected, int actual, string message );
Assert.AreNotEqual( int expected, int actual, string message,
                 params object[] parms );

Assert.AreNotEqual( long expected, long actual );
Assert.AreNotEqual( long expected, long actual, string message );
Assert.AreNotEqual( long expected, long actual, string message,
                 params object[] parms );

Assert.AreNotEqual( uint expected, uint actual );
Assert.AreNotEqual( uint expected, uint actual, string message );
Assert.AreNotEqual( uint expected, uint actual, string message,
                 params object[] parms );

Assert.AreNotEqual( ulong expected, ulong actual );
Assert.AreNotEqual( ulong expected, ulong actual, string message );
Assert.AreNotEqual( ulong expected, ulong actual, string message,
                 params object[] parms );

Assert.AreNotEqual( decimal expected, decimal actual );
Assert.AreNotEqual( decimal expected, decimal actual, string message );
Assert.AreNotEqual( decimal expected, decimal actual, string message,
                 params object[] parms );

Assert.AreNotEqual( float expected, float actual );
Assert.AreNotEqual( float expected, float actual, string message );
Assert.AreNotEqual( float expected, float actual, string message,
                 params object[] parms );

Assert.AreNotEqual( double expected, double actual );
Assert.AreNotEqual( double expected, double actual, string message );
Assert.AreNotEqual( double expected, double actual, string message,
                 params object[] parms );

Assert.AreNotEqual( object expected, object actual );
Assert.AreNotEqual( object expected, object actual, string message );
Assert.AreNotEqual( object expected, object actual, string message,
                 params object[] parms );

Comparing Numerics of Different Types

比较两个对象的方法重载做了特别规定,使不同类型可以用预期的数值进行比较。//这句话感觉翻译的糊里糊涂的

Assert.AreEqual( 5, 5.0 );可以成功的执行。

Comparing Floating Point Values

float和double类型使用一个表示了公差的额外参数,在公差范围内则表示相等。从NUnit2.4.4开始如果为提供第三个参数则使用GlobalSettings.DefaultFloatingPointTolerance 的值。在以前版本中如果未提供默认值,则会进行精确比较是否相等。

  NUnit对特殊性进行了处理,故一下会执行显示成功:

        Assert.AreEqual( double.PositiveInfinity, double.PositiveInfinity );
        Assert.AreEqual( double.NegativeInfinity, double.NegativeInfinity );
        Assert.AreEqual( double.NaN, double.NaN );    

  注意:最后一个例子与NUnit2.2.3有变化。在早期版本中,该测试会失败。由于新的方式在测试中会更有用所以进行了修改。为了避免混淆,建议在适当的时候使用新的Assert.IsNaN方法。

Comparing Arrays and Collections

从NUnit2.2开始,NUnit可以对两个一维数组进行比较。从NUnit2.4开始,多维数组、嵌套数组、集合都可以进行比较。通过Assert.AreEqual方法如果两个数组或者集合有相同的元素个数并且每个值相等那么则认为这两个数组或者集合相等。

Identity Asserts

Assert.AreSame and Assert.AreNotSame用于测试两个参数是否是一个引用。

Assert.AreSame( object expected, object actual );
Assert.AreSame( object expected, object actual, string message );
Assert.AreSame( object expected, object actual, string message, 
                params object[] parms );

Assert.AreNotSame( object expected, object actual );
Assert.AreNotSame( object expected, object actual, string message );
Assert.AreNotSame( object expected, object actual, string message, 
                params object[] parms );

Assert.Contains 测试一个数组或者集合是否包含一个对象。

Assert.Contains( object anObject, IList collection );
Assert.Contains( object anObject, IList collection, 
                string message );
Assert.Contains( object anObject, IList collection,
                string message, params object[] parms );

 

网站:feiger.cn         飞鸽博客,关注互联网、站长圈的程序员博客!