0

我们有一个 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

实际上它是相同的消息,但包含异常的名称。我和我的老板都不知道为什么会这样。

4

1 回答 1

0

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

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

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

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

于 2016-06-14T15:52:09.850 回答