SoapException 具有相同的消息,但形式不同

SoapException has the same message but in different forms

我们有一个 ASP.NET 项目。该项目是通过 InstallShield 安装的。我们有一个抛出 SoapException 并比较其消息的测试方法:

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

代码运行良好。但是我们决定 运行 在已安装的项目版本上进行此测试。问题是在这种情况下会抛出异常并显示消息:

 System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Exception: Rethrow exception, look at inner exception ---> System.ArgumentException: Item is invalid

其实是同一条消息,只是包含了异常的名称。我和我的老板都不知道为什么会这样。

我想知道是不是奇怪的 try/catch/rethrow 导致了这个问题。通常,使用 NUnit 时,不会捕获断言的异常。编写测试的更简单方法是...

var ex = Assert.Throws<SoapException>(() =>
{
    // Code that throws SoapException
}

Assert.That(ex.Message.StartsWith(...));

顺便说一句,我无法确定这是答案还是评论,但答案可以更轻松地格式化代码。 :-)