【问题标题】:SoapException has the same message but in different formsSoapException 具有相同的消息,但形式不同
【发布时间】:2016-06-14 07:50:00
【问题描述】:

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

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

【问题讨论】:

    标签: c# exception nunit installshield soapexception


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2012-06-09
      • 2017-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多