Contract.Requires< ArgumentException的>不要抛出userMessage [英] Contract.Requires<ArgumentException> do not throw userMessage

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

问题描述

我是代码合约的新手,目前我正在使用它们来检查我的例行程序。

I'm new to Code Contracts and at the moment I'm using them to check the percoditions of my routines.

我还需要测试它们以便我使用userMessage返回"错误"的名称。参数。 

I need also to test them so I use the userMessage to return the name of the "wrong" argument. 

我使用  SharpTestsEx进行测试。

I use SharpTestsEx for the tests.

例如: 

Contract.Requires< ArgumentNullException>(plant!= null," plant");

Contract.Requires<ArgumentNullException>(plant != null, "plant");

和类似的构造函数: 

and a constructor like: 

public static  ; myClass  CreateDowntimeForPlant(ProductionPlant plant,DateTime startingTime,DateTime endingTime)

public static  myClass CreateDowntimeForPlant(ProductionPlant plant, DateTime startingTime, DateTime endingTime)

通过以下方式测试:

 

&NBSP;&NBSP; &NBSP; &NBSP;   [TestMethod()]

        [TestMethod()]

   &NBSP; &NBSP;   public void  CreateDowntimeForPlant_should_throw_ArgumentNullException_on_Null_ProductionPlant()

        public void CreateDowntimeForPlant_should_throw_ArgumentNullException_on_Null_ProductionPlant()

   &NBSP; &NBSP;   {

        {

   &NBSP; &NBSP; &NBSP; &NBSP;   Executing.This(

            Executing.This(

           ()=>  myClass.CreateDowntimeForPlant( null,DateTime.Now,DateTime.Now.AddHours(1))

                    () => myClass.CreateDowntimeForPlant(null, DateTime.Now,DateTime.Now.AddHours(1))

         )。

                ).

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  应该()。

                Should().

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  投掷< ArgumentNullException>()。

                Throw<ArgumentNullException>().

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  和。

                And.

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   ValueOf。

                ValueOf.

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   ParamName。

                ParamName.

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  应该()。

                Should().

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   Be("plant");

                Be("plant");

   &NBSP; &NBSP;  }

        }

它正在运作。

 

另一方面,此类合约:  

On the other hand this type of contract: 

Contract.Requires< ArgumentException>(endingTime> startingTime," endingTime");

Contract.Requires<ArgumentException>(endingTime > startingTime, "endingTime");

不要抛出消息"endingTime" ;,但是一个空值,所以测试:

Do not throw the message "endingTime", but a null value so the test:

   &NBSP; &NBSP;   [TestMethod()]

        [TestMethod()]

   &NBSP; &NBSP;   public void  CreateDowntimeForPlant_should_throw_ArgumentException_if_EndingTime_is_lower_than_StartingTime()

        public void CreateDowntimeForPlant_should_throw_ArgumentException_if_EndingTime_is_lower_than_StartingTime()

   &NBSP; &NBSP;   {

        {

   &NBSP; &NBSP; &NBSP; &NBSP;   ProductionPlant plant = ProductionPlant.CreateProductionPlant(0," C"," Description");

            ProductionPlant plant = ProductionPlant.CreateProductionPlant(0, "C", "Description");

   &NBSP; &NBSP; &NBSP; &NBSP;   Executing.This(

            Executing.This(

           ()=> ProductionPlantDowntime.CreateDowntimeForPlant(plant, DateTime.Now,DateTime.Now.AddHours(-1))

                    () => ProductionPlantDowntime.CreateDowntimeForPlant(plant, DateTime.Now, DateTime.Now.AddHours(-1))

         )。

                ).

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  应该()。

                Should().

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  投掷< ArgumentException>()。

                Throw<ArgumentException>().

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  和。

                And.

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   ValueOf。

                ValueOf.

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   ParamName。

                ParamName.

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;  应该()。

                Should().

   &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;   Be("endingTime");

                Be("endingTime");

   &NBSP; &NBSP;  }

        }

无效,如果我更改了Should()。Be(" endingTime");与&NBSP;应()BE(NULL);测试运行没有错误,但我无法证明错误的论点。

is not working while, if I change Should().Be("endingTime"); with Should().Be(null); the test run with no error but I can not give evidence of the wrong argument.

 

 

推荐答案

是的,参数/字符串消息选择有点随机。对于ArgumentNullException和ArgumentOutOfRangeException,我们将字符串视为参数名称。除此之外,我们将其视为例外消息。

Yes, the parameter/string message selection is a bit random. For ArgumentNullException and ArgumentOutOfRangeException, we treat the string as the parameter name. For anything else, we treat it as the exception message.

 

 


这篇关于Contract.Requires&LT; ArgumentException的&GT;不要抛出userMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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