We have an ASP.NET project. The project is installed via InstallShield. We have a test method that throws SoapException and compares its message:
internal static string ExceptionMsgCheckForConflicts = "Server was unable to process request. ---> Rethrow exception, look at inner exception ---> System.ArgumentException ---> Item is invalid";
internal static string ErrorMsgCheckForConflictsInvalidException = "Exception should start with 'Server was unable to process request. ---> Rethrow exception, look at inner exception ---> System.ArgumentException ---> Item is invalid'";
[Test]
public void ConflictDetectorItemNotAnItemNode()
{
Assert.Throws<SoapException>(() =>
{
try
{
//Some code that throws SoapException
}
catch (SoapException ex)
{
Assert.IsTrue(ex.Message.StartsWith(ExceptionMsgCheckForConflicts, StringComparison.OrdinalIgnoreCase), ErrorMsgCheckForConflictsInvalidException);
throw;
}
});
}
The code works pretty well. But we decided to run this test on the installed version of project. The problem is that in this case exception is thrown with the message:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: Rethrow exception, look at inner exception ---> System.ArgumentException: Item is invalid
In fact it is the same message, but contains the names of exceptions. I and my boss have no idea why this happens.
I'm wondering if the odd try / catch / rethrow is causing the problem. Normally, using NUnit, one doesn't catch the exceptions one is asserting on. A simpler way to write the test would be...
var ex = Assert.Throws<SoapException>(() =>
{
// Code that throws SoapException
}
Assert.That(ex.Message.StartsWith(...));
BTW, I couldn't decide whether this was an answer or a comment, but answers make it easier to format code. :-)