【发布时间】:2011-05-01 00:51:42
【问题描述】:
目前,当我运行 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 团队?
-
我不会称其为“pex 论坛”,即使来自“他们”的人可能会在这里查看。看起来this 是论坛。
-
我认为他们不再回复了。在 pex 主页上,他们注意到论坛已移至 stackoverflow。 pex home page
-
Ups,应该多加注意。 (当然)甚至在旧论坛上也有一篇关于迁移到 SO 的帖子。对不起。
-
一切都好!我什至无法找到他们的旧论坛,所以谢谢。 :)
标签: c# c#-4.0 code-contracts pex