问题描述
当采用Code Contracts检查空参数值时,Pex不会忽略方法,因为没有检查参数的空值。
示例:
Pex keeps flunking out methods as not checking for null values for parameters when Code Contracts are employed to check for null parameter values.
Example:
/// <summary>
/// Bit shifts the enum bit pattern leftward.
/// </summary>
/// <param name="bitPattern"></param>
/// <param name="timesToShift">The number of times to shift the bit pattern.</param>
/// <returns></returns>
/// <seealso cref="http://msdn.microsoft.com/en-us/library/a1sway8w.aspx"/>
public static Enum
BitShiftLeft(this Enum bitPattern, int timesToShift) {
Contract.Requires(bitPattern != default(Enum));
Contract.Requires(EnumFlagsUtilities.DoesEnumHaveFlagsAttribute(bitPattern), ErrorMessageUtils.EnumNotAFlagsEnumMessage);
int enumAsAnInt = Convert.ToInt32(bitPattern);
enumAsAnInt <<= timesToShift;
Enum result = (Enum)Convert.ChangeType(enumAsAnInt, bitPattern.GetType());
return result;
}
推荐答案
那些违反顶级Requires子句的测试用例应该被标记为"成功"和"rdquo;"。他们不是吗?
有一些属性,例如
PexAllowedContractRequiresFailureAttribute ,
PexAllowedContractRequiresFailureAtTypeUnderTestSurfaceAttribute ,用于控制合同失败
的分类方式(" ;失败" vs."成功")。
>问题是,我声明如果用户试图通过声明一个需要bitpattern的合同来做到这一点,那将会发生什么!=
default(Enum)!
好吧,从某种意义上说,你宣布将要发生的一切:毕竟,
你写了代码。如果有什么东西,bug会进入发生在
实际行为偏离预期的行为的地方。从这个意义上说,Pex生成这些测试用例是件好事。有时它们似乎是有点多余,但他们只是展示实际会发生什么。如果这是你所期望的那么多,那么我会认为这是一件好事。(否则,这是一个错误。)
Well, in some sense you declare everything that will happen: After all,you wrote the code. Bugs creep in if something happens where theactual behavior deviates from your intended behavior. In that sense, it's a good thing that Pex generates these test cases. They might sometimes appear to be a bit redundant, but they just show what would actually happen. If that's what you expect, then I would consider that to be a good thing. (Otherwise, it's a bug.)
错误可以在实际代码中,也可以在合同中.Pex分析两者。
这篇关于Pex抱怨“缺陷”因违反方法声明的代码合同而被暴露的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!