Contract.Requires抛出pex错误

Jos*_*ale 26 c# pex code-contracts c#-4.0

可能重复:
如何配置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");
}
Run Code Online (Sandbox Code Playgroud)

失败的方法调用:

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,
               };
}
Run Code Online (Sandbox Code Playgroud)

---描述

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);
}
Run Code Online (Sandbox Code Playgroud)

And*_*rew 0

根据我对 Pex 的有限经验,我的理解是这些Contract方法定义了达到它们所在方法的先决条件。所以,当你说

Contract.Requires(!string.IsNullOrWhiteSpace(username));
Run Code Online (Sandbox Code Playgroud)

您是说应该无法使用空或空白用户名参数来访问该语句。Pex 基本上是在说你错了。这是 Pex真正擅长的一件事。这意味着您有可能在对您的方法的某些调用中NullReferenceException没有检查空/空白。那么,你的任务就是找到位置。您可以通过处理方法中的空/空白,然后删除 for它来解决问题,或者通过确保所有调用者传递非空、非空用户名来解决问题。我认为更好的选择取决于您的情况,但几乎在所有情况下,我都会在方法中处理空/空白用户名。这样,您就可以在代码中的一个位置优雅地处理错误。usernameCreateUserusernameCreateUserContract.RequiresCreateUserCreateUser

当然,您确实应该查看哪个调用者可以传递 null 或空格,因为这可能表明用户输入验证问题以及其他潜在问题。