【发布时间】:2013-04-21 04:51:13
【问题描述】:
我有一个简单的方法,可以返回给定数字的指数值:
public int Exp(int num)
{
return Convert.ToInt32(System.Math.Exp(num));
}
运行 Pex 时,我在 Summary/Exception 字段中得到一个 OverFlowException,用于某个大数字:1969057606。
如何使用Contract.Ensure() 创建发布条件?
我尝试了以下方法,但它没有做任何事情:
Contract.Ensures(Contract.Result<int>() < 2147483647);
// This is because the max value an int variable can hold is 2147483647
【问题讨论】:
-
你应该使用
int.MaxValue而不是实际的数字,这对于查看代码的人来说更容易阅读和解析。 -
非常感谢加布里埃尔。那是真实的!感谢您的帮助!
标签: class pex contract overflowexception