可能重复:
How Do You Configure Pex to Respect Code Contracts?Contract.Requires抛出PEX错误
目前,当我运行一个PEX的探索,我在班级创建的代码合同被视为错误pex勘探结果。我认为,当你使用代码合同进行pex探索时,合同失败应该被视为预期的行为。 以下是导致异常的代码。
测试方法:
[PexMethod]
public void TestEquality(Guid userId, string username, string password, string securityQuestion, string securityAnswer)
{
UserSecurity user = UserTools.CreateUser(Guid.NewGuid(), username, password, securityQuestion, securityAnswer);
bool passwordResult = UserTools.VerifyInput(password, user.Password, user.PasswordSalt);
bool securityAnswerResult = UserTools.VerifyInput(securityAnswer, user.SecurityAnswer, user.SecurityAnswerSalt);
Assert.IsTrue(passwordResult, "Password did not correctly re-hash");
Assert.IsTrue(securityAnswerResult, "Security Answer did not correctly re-hash");
}
失败的方法调用:
public static UserSecurity CreateUser(Guid userId, string username, string password, string securityQuestion, string securityAnswer)
{
Contract.Requires(userId != Guid.Empty);
Contract.Requires(!string.IsNullOrWhiteSpace(username));
Contract.Requires(!string.IsNullOrWhiteSpace(password));
Contract.Requires(!string.IsNullOrWhiteSpace(securityQuestion));
Contract.Requires(!string.IsNullOrWhiteSpace(securityAnswer));
Contract.Ensures(Contract.Result<UserSecurity>() != null);
byte[] passwordSalt;
byte[] securityAnswerSalt;
return new UserSecurity
{
UserId = userId,
Username = username,
Password = SecurityUtilities.GenerateHash(password, out passwordSalt),
PasswordSalt = passwordSalt,
SecurityQuestion = securityQuestion,
SecurityAnswer = SecurityUtilities.GenerateHash(securityAnswer, out securityAnswerSalt),
SecurityAnswerSalt = securityAnswerSalt,
};
}
---说明
failing test: ContractException, Precondition failed: !string.IsNullOrWhiteSpace(username)
Guid s0
= new Guid(default(int), (short)32, (short)32, default(byte), default(byte),
default(byte), default(byte), default(byte),
default(byte), default(byte), default(byte));
this.TestEquality(s0, (string)null, (string)null, (string)null, (string)null);
[TestMethod]
[PexGeneratedBy(typeof(HashTests))]
[PexRaisedContractException]
public void TestEqualityThrowsContractException173()
{
Guid s0
= new Guid(default(int), (short)32, (short)32, default(byte), default(byte),
default(byte), default(byte), default(byte),
default(byte), default(byte), default(byte));
this.TestEquality(s0, (string)null, (string)null, (string)null, (string)null);
}
PEX团队是否会监督这个论坛?还是没有更多的PEX团队? – 2011-05-05 01:39:15
我不会称之为“pex论坛”,即使来自“他们”的人可能会在此处查看。看起来像[这](http://social.msdn.microsoft.com/Forums/en-US/pex/threads/)是论坛。 – 2011-05-09 12:33:20
我不认为他们在那里回应。在pex主页上,他们记下论坛已被移至stackoverflow。 [PEX主页](http://research.microsoft.com/en-us/projects/pex/) – 2011-05-10 01:26:02