2016-06-14 123 views
0

我们有一个ASP.NET项目。该项目通过InstallShield安装。我们有一个抛出SoapException并且比较它的消息的测试方法: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 

其实,这是相同的消息,但包含例外的名称。我和我的老板不知道为什么会发生这种情况。

回答

0

我想知道是否奇怪的try/catch/rethrow是造成这个问题。通常情况下,使用NUnit,我们无法捕捉到所宣称的异常。更简单的方法来编写测试会...

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

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

顺便说一句,我不能确定这是否是一个答案或评论,但答案更容易地格式化代码。 :-)

相关问题