Contract.Requires抛出PEX错误 [英] Contract.Requires throwing pex errors

查看:235
本文介绍了Contract.Requires抛出PEX错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
如何配置Pex的尊重代码契约?






目前,当我运行一个PEX的探索,我在班级创建的代码合同被视为在PEX勘探结果的误差。我以为当您运行使用代码契约合同失效应被视为预期行为PEX探索。
这是导致异常的代码



测试方法:

  [PexMethod] 
公共无效TestEquality(GUID userid,字符串的用户名,密码字符串,字符串securityQuestion,串securityAnswer)
{
UserSecurity用户= UserTools.CreateUser(Guid.NewGuid( ),用户名,密码,securityQuestion,securityAnswer);

布尔passwordResult = UserTools.VerifyInput(密码,user.password的,user.PasswordSalt);
布尔securityAnswerResult = UserTools.VerifyInput(securityAnswer,user.SecurityAnswer,user.SecurityAnswerSalt);

Assert.IsTrue(passwordResult,密码不正确地重新哈希);
Assert.IsTrue(securityAnswerResult,安全答案没有正确地重新哈希);
}



失败的方法调用:

 公共静态UserSecurity CREATEUSER(GUID userid,字符串的用户名,密码字符串,字符串securityQuestion,串securityAnswer)
{
Contract.Requires(用户ID!= GUID。空);
Contract.Requires(string.IsNullOrWhiteSpace(用户名)!);
Contract.Requires(string.IsNullOrWhiteSpace(密码)!);
Contract.Requires(string.IsNullOrWhiteSpace(securityQuestion)!);
Contract.Requires(string.IsNullOrWhiteSpace(securityAnswer)!);
Contract.Ensures(Contract.Result< UserSecurity>()!= NULL);

字节[] passwordSalt;
字节[] securityAnswerSalt;

返回新UserSecurity
{
用户ID =用户id,
=用户名用户名,
密码= SecurityUtilities.GenerateHash(密码,出passwordSalt),
passwordSalt = passwordSalt,
securityQuestion = securityQuestion,
securityAnswer = SecurityUtilities.GenerateHash(securityAnswer,出securityAnswerSalt),
securityAnswerSalt = securityAnswerSalt,
};
}



---说明

 失败的测试:ContractException,前提条件失败:string.IsNullOrWhiteSpace(用户名)

的Guid S0
=新的GUID(默认值(INT) (短)32,(短)32,默认值(字节),默认值(字节),
默认值(字节),默认值(字节),默认值(字节),
默认值(字节),默认(字节),默认值(字节));
this.TestEquality(S0,(串)空(字符串)空(字符串)空(字符串)NULL);


[TestMethod的]
[PexGeneratedBy(typeof运算(HashTests))]
[PexRaisedContractException]
公共无效TestEqualityThrowsContractException173()
{
的Guid S0
=新的GUID(默认值(INT),(短)32,(短)32,默认值(字节),默认值(字节),
默认值(字节),默认值(字节),默认值(字节),
默认值(字节),默认值(字节),默认值(字节));
this.TestEquality(S0,(串)空(字符串)空(字符串)空(字符串)NULL);
}


解决方案

我觉得,如果你使用。标准合同重写,失败的勾去掉断言,让使用类型化的代码thron ArgumentNullException需要参数

  contract.Requires< ArgumentNullException> (!I = NULL); 

当你这样做的方法将抛出argumentnullexceptions ... PEX表现非常顺利地使用它们。



在编译的时候,你仍然可以得到合同的检查和你所期望的静态检查。



它看起来像PexRaisedContractException ISN T与你如何使用它的行为。我不能说我使用的属性虽然。我从你的角度想我的方法是一个变通;)



编辑:Pex公司应该产生这样的测试,但测试应该抛出的错误,这应该会导致测试通过。这种不正常的事实表明,我认为重写不工作,或者被抛出的异常是不是该属性正在寻找异常类型。


Possible Duplicate:
How Do You Configure Pex to Respect Code Contracts?

Currently, when I run a pex exploration, the code contracts I created in my classes are being treated as errors in the pex exploration results. I thought when you ran pex exploration using code contracts the contract failures should be treated as expected behavior. Here is the code causing the exceptions.

Test Method:

[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");
}

Failing method call:

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,
               };
}

--- Description

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);
}

解决方案

I find that if you use standard contract rewriter, untick assert on failure and let your code thron ArgumentNullException by using a typed Requires argument.

contract.Requires<ArgumentNullException>(i!=null);

when you do this the methods will throw argumentnullexceptions ... pex behaves perfectly well with them.

At compile time you still get contract checking and static checking as you would expect.

It does look like PexRaisedContractException isn't behaving with how you use it. I can't say i use that attribute though. I guess from your perspective my way is a work around ;)

EDIT: Pex should generate this test, but the test should throw the error and that should cause the test to pass. the fact this is not working suggests to me that the rewriter isn't working or that the exception being thrown is not the exception type that the attribute is looking for.

这篇关于Contract.Requires抛出PEX错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆