通过param名称对ArgumentNullException进行单元测试(unit testing for ArgumentNullException by param name)
我有一个单元测试,并且正在检查我的控制器构造函数的空例外情况以用于几种不同的服务。
[TestMethod] [ExpectedException(typeof(ArgumentNullException))]在我的控制器构造函数中,我有:
if (routeCategoryServices == null) throw new ArgumentNullException("routeCategoryServices"); if (routeProfileDataService == null) throw new ArgumentNullException("routeProfileDataService");我有每个单元测试,但我怎么能区分这两个。 我可以保持测试,因为任何一个检查都可能抛出null,所以我想通过param名称测试异常。
这可能吗?
I have a unit test and am checking for null exceptions of my controller constructor for a few different services.
[TestMethod] [ExpectedException(typeof(ArgumentNullException))]In my controller constructor I have:
if (routeCategoryServices == null) throw new ArgumentNullException("routeCategoryServices"); if (routeProfileDataService == null) throw new ArgumentNullException("routeProfileDataService");I have a unit test for each, but how can I distinguish between the two. I can leave the test as is as either of the checks could be throwing null so I want to test the exception by param name.
Is this possible?
最满意答案
您可以明确地在您的测试中捕获异常,然后断言ParamName属性的值:
try { //test action } catch(ArgumentException ex) { Assert.AreEqual(expectedParameterName, ex.ParamName); }You could explicitly catch the exception in your test and then assert the value of the ParamName property:
try { //test action } catch(ArgumentException ex) { Assert.AreEqual(expectedParameterName, ex.ParamName); }更多推荐
发布评论