Build/Test Issues

Not running custom ExpectedExceptionAttribute with MSTest

Started by giantdave on 5,577 views

I have a custom expected exception attribute which derives from ExpectedExceptionBaseAttribute

NCrunch (1.46.0.30) is showing the test as a failure when an exception is thrown

Resharper (7.3) and the VS2012 test runner both show the test as passing

    public sealed class HttpResponseExceptionExpectedAttribute : ExpectedExceptionBaseAttribute
    {
        private readonly HttpStatusCode _expectedCode;

        public HttpResponseExceptionExpectedAttribute(HttpStatusCode expectedCode) : this(expectedCode, "No exception was thrown") { }

        public HttpResponseExceptionExpectedAttribute(HttpStatusCode expectedCode, string noExceptionMessage)
            : base(noExceptionMessage)
        {
            _expectedCode = expectedCode;
        }

        protected override void Verify(Exception exception)
        {
            Assert.IsNotNull(exception);
            Assert.IsInstanceOfType(exception, typeof(HttpResponseException), "Wrong type of exception thrown");
            var responseException = exception as HttpResponseException;
            Assert.AreEqual(_expectedCode, responseException.Response.StatusCode);
        }
    }


        [TestMethod]
        [HttpResponseExceptionExpected(HttpStatusCode.Forbidden)]
        public void ExampleTest()
        {
            throw new HttpResponseException(HttpStatusCode.Forbidden);
        }
Hi, thanks for posting!

I'm afraid that NCrunch currently does not support custom ExpectedException attributes for the MSTest framework.

This is due to limitations in the way that NCrunch is able to interoperate with this framework. Because NCrunch's MSTest adapter only emulates MSTest (rather than calling it directly), not all features of the framework can be made available. I recommend implementing an alternative method for inspecting exceptions, such as a try->catch block within the test itself.


Cheers,

Remco

Post a reply

Log in to reply.