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

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

问题描述

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

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()

您还可以像这样检查 ExpectedException 中包含的消息:

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()

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

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()

编译器会报如下错误:

属性参数必须是常量表达式,typeof 表达式或数组创建表达式属性

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:

  • 是测试的一部分,在这种情况下,从资源中获取它是错误的"(否则您最终可能会得到一致损坏的资源),因此只需在更改资源时更新测试(或测试失败))
  • 不是测试的一部分,您应该只关心它是否会引发异常.

请注意,第一个选项应该允许您测试多种语言,前提是能够使用语言环境运行.

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

至于多个异常,我来自 C++ 领域,在那里创建大量异常(到每个抛出"语句一个!)在大范围内是可以接受的(如果不常见的话),但是 .Net 的元数据系统可能不喜欢这样,因此建议.

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天全站免登陆