4

I am quite new to TDD and am going with NUnit and Moq. I have got a method where I expect an exception, so I wanted to play a little with the frameworks features.

My test code looks as follows:

    [Test]
    [ExpectedException(ExpectedException = typeof(MockException), ExpectedMessage = "Actual differs from expected")]
    public void Write_MessageLogWithCategoryInfoFail()
    {
        string message = "Info Test Message";

        Write_MessageLogWithCategory(message, "Info");

        _LogTest.Verify(writeMessage =>
            writeMessage.Info("This should fail"),
            "Actual differs from expected"
        );
    }

But I always receive the errormessage that the error message that the actual exception message differs from the expected message. What am I doing wrong?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
Mark
  • 7,891
  • 5
  • 27
  • 36

2 Answers2

3

Unfortunately Resharper test runner has a bug/limitation - it doesn't handle the ExpectedException attributes. You have 2 options:

  1. Use some other test runner (e.g. nunit-gui.exe, shipped with NUnit) - but this approach makes it a pain to debug your tests

  2. Catch and validate the exception manually, using the following pattern:

    [Test] public void Write_MessageLogWithCategoryInfoFail() { try { string message = "Info Test Message";

      Write_MessageLogWithCategory(message, "Info");
    
      _LogTest.Verify(writeMessage =>
          writeMessage.Info("This should fail"),
          "Actual differs from expected"
      );
      Assert.Fail("Expected exception");
    }
    catch(MockException e)
    {
      Assert.AreEqual("Actual differs from expected", e.Message);
    }
    

    }

Its a real shame, because the descriptive way of saying that you expect an exception is much nicer!

On a side note I hope that the code above is only for playing with the framework - usually you would never catch MockExceptions :)

Grzenio
  • 35,875
  • 47
  • 158
  • 240
  • 1
    Thanks I tested your code and I think I know now why it never was equal. The message in the exception is larger due to a more detailed error message: "Actual differs from expected\r\nInvocation was not performed on the mock: writeMessage => writeMessage.Info(\"This should fail\")" But thanks for the hint! And even though this test runs over production code.. Yes I'm playing around with framework ;-) – Mark Jun 09 '10 at 15:05
  • I got a similar error running an NUnit test with TestDrivet.Net: The exception message text was incorrect Expected: Password_too_short but was: Password_too_short EDIT: in my case the message was really different (continued after a line break) – Cristian Lupascu Aug 23 '11 at 08:16
  • @w0lf, you mean that there was a line break at the end of the exception message, but not in what you passed in TestDriven.NET? – Jennifer S Feb 07 '12 at 17:07
  • @JenniferS Yes. I somehow saw the messages side-by-side in the NUnit output and they seemed equal. But then I figured it out... – Cristian Lupascu Feb 07 '12 at 17:27
  • The ReSharper Test runner as of today handles the attributes. I have version JetBrains ReSharper 7.1.3 C# Edition Build 7.1.3000.2254 using Visual Studio 2010 (Version 10.0.40219.1) – Marcel Dec 27 '13 at 14:49
  • @Grzenio Please consider my comment about ReSharper – Marcel Dec 27 '13 at 14:57
  • @Marcel, they must have fixed it at last! – Grzenio Dec 30 '13 at 09:39
2

You may use the extra parameter MatchType = MessageMatch.Regex.

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
Chris
  • 29
  • 2