本文介绍了断言失败并在vb.net 2008中尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码始终会正确失败.

< testmethod()> _
Public Sub AlwaysFails()
Assert.Fail(这总是失败")
结束子

但是,这永远不会失败,我认为是错误的:

< testmethod()> _
Public Sub NeverFails()
试试
Assert.Fail(这永远不会失败")
异常捕获

结束尝试
结束子

为什么?如果这是设计使然,那么我想我应该永远不要使用Assert.Fail.

This code always fails, correctly.

<testmethod()> _
Public Sub AlwaysFails()
Assert.Fail("this always fails")
End Sub

However, this never fails, incorrectly (in my opinion):

<testmethod()> _
Public Sub NeverFails()
Try
Assert.Fail("This never fails")
Catch ex As Exception

End Try
End Sub

Why? If this is by design, then I think I should probably never use Assert.Fail.

推荐答案



<br />
<TestMethod()> _<br />
Public Sub AlwaysPasses()<br />
Try<br />
  SubThatIsSupposedToThrowAnExceptionButDoesNot()<br />
  Assert.Fail("Sub did not throw an exception")<br />
Catch ex As Exception<br />
<br />
End Try<br />
End Sub<br />



但是以下代码将正常运行:



But the following code will work properly:

<br />
<TestMethod()> _<br />
Public Sub WorksCorrectly()<br />
Dim bolFailed as Boolean = False<br />
Try<br />
  SubThatIsSupposedToThrowAnExceptionButDoesNot()<br />
  bolFailed = True<br />
Catch ex As Exception<br />
<br />
End Try<br />
If bolFailed Then<br />
  Assert.Fail("Sub did not throw an exception")<br />
End If<br />
End Sub<br />



这意味着您在使用Assert.Fail时必须非常小心,因为它不会总是导致测试失败.如果Assert.Fail在try块内,则永远不会导致测试失败.



That means you have to be very careful about using Assert.Fail, because it will not always cause the test to fail. If Assert.Fail is within a try block, it will NEVER NEVER NEVER cause your test to fail.


这篇关于断言失败并在vb.net 2008中尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 08:32