Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Not running custom ExpectedExceptionAttribute with MSTest
giantdave
#1 Posted : Friday, August 23, 2013 10:54:51 AM(UTC)
Rank: Newbie

Groups: Registered
Joined: 8/23/2013(UTC)
Posts: 1
Location: United Kingdom

Thanks: 1 times
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

Code:
    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);
        }
    }


Code:
        [TestMethod]
        [HttpResponseExceptionExpected(HttpStatusCode.Forbidden)]
        public void ExampleTest()
        {
            throw new HttpResponseException(HttpStatusCode.Forbidden);
        }
Remco
#2 Posted : Friday, August 23, 2013 11:21:26 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,144

Thanks: 959 times
Was thanked: 1290 time(s) in 1196 post(s)
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
1 user thanked Remco for this useful post.
giantdave on 8/23/2013(UTC)
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.026 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download