How Can I Test For An Expected Exception With A Specific Exception Message From A Resource File In Visual Studio Test?
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.
- → React Native with visual studio 2015 IDE
- → Where to find example Visual Studio 2015 project(s) using typescript and React?
- → How to configure Visual Studio 2015 to write JSX in ASP.NET 5 (RC) with intellisense
- → How to make this simple TypeScript + React app work with Visual Studio 2015?
- → Format liquid(Shopify) code in visual studio code
- → Typescript intellisense without emitting require
- → How to display shopify free them output on my laptop using visual studio code?
- → Visual Studio Code - how to debug node.js routes?
- → Visual Studio cordova, simple way to "live reload" on Android devices?
- → --allow-live terminal command for shopify development
- → compile typescript with a visual studio code task, specifying the output directory
- → How to highlight all symbol appearances in python code with PTVS?
- → What is the location of the Python tools for Visual Studio?