How can I test for an expected exception with a specific exception message from a resource file in Visual Studio Test?

前端 未结 7 1937
广开言路
广开言路 2020-12-13 09:11

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

[TestMethod]
[ExpectedExc         


        
7条回答
  •  不思量自难忘°
    2020-12-13 09:33

    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.

提交回复
热议问题