Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I test for an expected exception with a specific exception message from a resource file in Visual Studio Test?
    primarykey
    data
    text
    <p>Visual Studio Test can check for expected exceptions using the ExpectedException attribute. You can pass in an exception like this:</p> <pre><code>[TestMethod] [ExpectedException(typeof(CriticalException))] public void GetOrganisation_MultipleOrganisations_ThrowsException() </code></pre> <p>You can also check for the message contained within the ExpectedException like this:</p> <pre><code>[TestMethod] [ExpectedException(typeof(CriticalException), "An error occured")] public void GetOrganisation_MultipleOrganisations_ThrowsException() </code></pre> <p>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:</p> <pre><code>[TestMethod] [ExpectedException(typeof(CriticalException), MyRes.MultipleOrganisationsNotAllowed)] public void GetOrganisation_MultipleOrganisations_ThrowsException() </code></pre> <p>The compiler will give the following error:</p> <blockquote> <p>An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute</p> </blockquote> <p>Does anybody know how to test for an exception that has a message from a resource file?</p> <hr> <p>One option I have considered is using custom exception classes, but based on often heard advice such as:</p> <blockquote> <p>"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." <a href="http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx" rel="noreferrer">Source</a></p> </blockquote> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload