Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MSTest ExpectedException failing

I usually use Nunit but in my current project I am using MSTest. Now I have a test which expects an exception but keeps failing, and I do not have any idea as to why.

Here is a simple example I have used to replicate the problem:

[TestMethod, ExpectedException(typeof(ErrorResponseException))]
        public void should_throw_exception()
        {
            throw new ErrorResponseException();
        }

The ErrorResponseException is a class that just inherits from Exception, that is it, anyone know why this is failing, I would expect it to pass.

like image 244
Grofit Avatar asked Nov 03 '11 12:11

Grofit


2 Answers

In Visual Studio 15with dependencies on MSTest.TestAdapter v1.1.18 and MSTest.TestFramework v1.1.18 you could also use

Assert.ThrowsException<ArgumentNullException>(() => MethodThatThrowsArgumentNullException());

like image 198
Orhan Avatar answered Oct 02 '22 12:10

Orhan


Shameless plug, but I wrote a simple assembly that makes asserting for exceptions and exception messages a little easier and more readable in MSTest using Assert.Throws() syntax. I wrote a blog post with the full details.

like image 39
Bradley Braithwaite Avatar answered Oct 02 '22 12:10

Bradley Braithwaite