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

Notification

Icon
Error

Custom ExpectedExceptionBaseAttribute not treated like ExpectedExceptionAttribute
DougC
#1 Posted : Wednesday, May 22, 2019 6:44:28 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/22/2019(UTC)
Posts: 2
Location: United States of America

Thanks: 1 times
My unit tests are built using Visual Studio 2017/19/MSTest.

I have a customized version of the ExpectedExceptionBaseAttribute that checks both the Exception thrown but also the Message to ensure it is the expected value. Using it looks like this:

Code:
[TestMethod]
[ExpectedExceptionMessage(typeof(InvalidOperationException), "This is the Message I expect.")]
public void MyUnitTest() { … }

All unit tests using this attribute are shown as failures by NCrunch. Could you please refactor your code to look for the ExpectedExceptionBaseAttribute rather than ExpectedExceptionAttribute? Thanks!

Here is the full source of my ExpectedExceptionMessageAttribute class:

Code:
/// <summary>
/// Ensures that a unit test throws the given exception and that the exception's message is a given value.
/// </summary>
public sealed class ExpectedExceptionMessageAttribute : ExpectedExceptionBaseAttribute
{
    readonly string ExpectedExceptionMessage;
    readonly Type ExpectedExceptionType;
    /// <summary>
    /// Initializes a new instance of <see cref="ExpectedExceptionMessageAttribute"/>.
    /// </summary>
    /// <param name="expectedExceptionType">The Type of the Exception the unit test should throw.</param>
    /// <param name="expectedExceptionMessage">The expected value of the Message property of the Exception thrown.</param>
    public ExpectedExceptionMessageAttribute(Type expectedExceptionType, string expectedExceptionMessage)
    {
        ExpectedExceptionType = expectedExceptionType;
        ExpectedExceptionMessage = expectedExceptionMessage;
    }
    /// <summary>
    /// Called when a unit test throws an unhandled exception.
    /// </summary>
    /// <param name="exception">The Exception that was thrown.</param>
    protected override void Verify(Exception exception)
    {
        Assert.IsNotNull(exception);

        // We're not interested in assertion failures in the test method
        base.RethrowIfAssertException(exception);

        // Make sure the exception thrown is of the expected type
        Assert.IsInstanceOfType(exception, ExpectedExceptionType, "The exception thrown is the wrong type.");

        // Make sure the exception's message matches
        Assert.AreEqual(ExpectedExceptionMessage, exception.Message,
            string.Format("{0}.Message was not expected value.", exception.GetType().Name));
    }
}
Remco
#2 Posted : Thursday, May 23, 2019 12:24:46 AM(UTC)
Rank: NCrunch Developer

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

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Hi Doug,

Thanks for sharing this issue.

NCrunch's support for MSTest is provided using an emulator rather than integrating direct with the toolset itself (this is done for a range of reasons, mostly because MSTest has generally been horrible to integrate with over the years).

The problem with using an emulator rather than integrating is that we need to implement behaviour without calling into the base MS code, such as the Exception's verify method. So there's no way we can safely support test code that extends the MSTest attributes in this manner. I recommend implementing your code in a different way, perhaps using your own service call of some kind.
1 user thanked Remco for this useful post.
DougC on 5/23/2019(UTC)
DougC
#3 Posted : Thursday, May 23, 2019 12:09:41 PM(UTC)
Rank: Newbie

Groups: Registered
Joined: 5/22/2019(UTC)
Posts: 2
Location: United States of America

Thanks: 1 times
Hi Remco,

Thanks for your quick response. Unfortunately, we have too many unit tests that rely on this behavior, so it looks like NCrunch might not work for us. That's quite disappointing because I've been thoroughly impressed with your product!

Best regards,
Doug
Remco
#4 Posted : Thursday, May 23, 2019 10:22:06 PM(UTC)
Rank: NCrunch Developer

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

Thanks: 964 times
Was thanked: 1296 time(s) in 1202 post(s)
Hi Doug,

I'm sorry to hear that! I wish you all the best.
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.033 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download