如何使用Visual Studio测试中的资源文件中的特定异常消息来测试预期异常? [英] How can I test for an expected exception with a specific exception message from a resource file in Visual Studio Test?

查看:132
本文介绍了如何使用Visual Studio测试中的资源文件中的特定异常消息来测试预期异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio Test可以使用ExpectedException属性检查预期的异常。你可以传入这样的异常:

  [TestMethod] 
[ExpectedException(typeof(CriticalException))]
public void GetOrganisation_MultipleOrganisations_ThrowsException()

您还可以检查ExpectedException中包含的消息,如下所示:

  [TestMethod] 
[ExpectedException(typeof(CriticalException),发生错误)]
public void GetOrganisation_MultipleOrganisations_ThrowsException()

但是,当测试I18N应用程序时,我将使用资源文件来获取该错误消息(任何人甚至可能决定测试错误消息的不同本地化,如果我想,但Visual Studio不会让我这样做:

  [TestMethod] 
[ExpectedException(Typeof(CriticalException))MyBaseBoolean >

编译器将给出以下错误:


属性参数必须是
常量表达式,typeof expression

属性的数组创建表达式


有人知道如何测试具有消息的异常从资源文件?






我考虑过的一个选项是使用自定义异常类,但是基于经常听到的建议,如:


创建并抛出自定义异常
如果您有错误条件
可以以编程方式处理一个
与任何其他现有
异常不同的方式。否则,抛出
之一的现有异常。来源


我不期望在正常流程中处理异常(这是一个关键的例外,所以我是无论如何,我不认为为每个测试用例创建一个异常是正确的事情。任何意见?

解决方案

只是意见,但我会说错误文字:




  • 是测试的一部分在这种情况下,从资源获取它将是错误的(否则可能会导致一个一致的资源),所以只需更新测试,当您更改资源(或测试失败)

  • 不是测试的一部分,您只应该注意它会引发异常。



请注意,第一个选项应该让你测试多种语言,给定对于多个异常,我来自C ++土地,在这里创建加载和加载的异常(到每个'throw一个点')大声明中的'声明!)是可以接受的(如果不是常见的话),但是.Net的元数据系统可能不喜欢,所以建议。


Visual Studio Test can check for expected exceptions using the ExpectedException attribute. You can pass in an exception like this:

[TestMethod]
[ExpectedException(typeof(CriticalException))]
public void GetOrganisation_MultipleOrganisations_ThrowsException()

You can also check for the message contained within the ExpectedException like this:

[TestMethod]
[ExpectedException(typeof(CriticalException), "An error occured")]
public void GetOrganisation_MultipleOrganisations_ThrowsException()

But when testing I18N applications I would use a resource file to get that error message (any may even decide to test the different localizations of the error message if I want to, but Visual Studio will not let me do this:

[TestMethod]
[ExpectedException(typeof(CriticalException), MyRes.MultipleOrganisationsNotAllowed)]
public void GetOrganisation_MultipleOrganisations_ThrowsException()

The compiler will give the following error:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute

Does anybody know how to test for an exception that has a message from a resource file?


One option I have considered is using custom exception classes, but based on often heard advice such as:

"Do create and throw custom exceptions if you have an error condition that can be programmatically handled in a different way than any other existing exception. Otherwise, throw one of the existing exceptions." Source

I'm not expecting to handle the exceptions differently in normal flow (it's a critical exception, so I'm going into panic mode anyway) and I don't think creating an exception for each test case is the right thing to do. Any opinions?

解决方案

Just an opinion, but I would say the error text:

  • is part of the test, in which case getting it from the resource would be 'wrong' (otherwise you could end up with a consistantly mangled resource), so just update the test when you change the resource (or the test fails)
  • is not part of the test, and you should only care that it throws the exception.

Note that the first option should let you test multiple languages, given the ability to run with a locale.

As for multiple exceptions, I'm from C++ land, where creating loads and loads of exceptions (to the point of one per 'throw' statement!) in big heirachies is acceptable (if not common), but .Net's metadata system probably doesn't like that, hence that advice.

这篇关于如何使用Visual Studio测试中的资源文件中的特定异常消息来测试预期异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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