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

前端 未结 7 1949
广开言路
广开言路 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:36

    I wonder if NUnit is moving down the path away from simplicity... but here you go.

    New enhancements (2.4.3 and up?) to the ExpectedException attribute allow you more control on the checks to be performed on the expected Exception via a Handler method. More Details on the official NUnit doc page.. towards the end of the page.

    [ExpectedException( Handler="HandlerMethod" )]
    public void TestMethod()
    {
    ...
    }
    
    public void HandlerMethod( System.Exception ex )
    {
    ...
    }
    

    Note: Something doesn't feel right here.. Why are your exceptions messages internationalized.. Are you using exceptions for things that need to be handled or notified to the user. Unless you have a bunch of culturally diverse developers fixing bugs.. you shouldn't be needing this. Exceptions in English or a common accepted language would suffice. But in case you have to have this.. its possible :)

提交回复
热议问题