我们有一个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实际上,它是相同的消息,但包含异常的名称。我和我的老板不知道为什么会这样。
发布于 2016-06-14 15:52:09
我想知道奇怪的尝试/捕捉/重抛是否导致了这个问题。通常,使用NUnit时,不会捕获断言的异常。写测试的更简单的方法是..。
var ex = Assert.Throws<SoapException>(() =>
{
// Code that throws SoapException
}
Assert.That(ex.Message.StartsWith(...));顺便说一句,我无法决定这是一个回答还是一个评论,但答案使代码的格式更容易。:-)
https://stackoverflow.com/questions/37806038
复制相似问题