断言失败并在vb.net 2008中尝试 [英] Assert.Fail and Try in vb.net 2008

查看:80
本文介绍了断言失败并在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.

推荐答案

我认为它总是会失败,但是应该注意,默默地阻止所有异常.此方法因抛出异常而失败,但是您看不到它(双关语是故意的).

请通过在调试器下逐步运行和/或处理捕获的异常来对其进行检查.

我知道这只是实验性的,但是在现实生活中,您永远都不应捕捉到这样的异常,从而阻止其进一步传播.此外,过度使用try-catch会导致异常目的失败.在大多数情况下,您需要在每个线程的栈顶捕获异常.也在顶级UI周期中.

我也建议使用System.Diagnostics.Debug.Assert进行断言.

-SA
I think it always fail, as it should, but pay attention that you''re blocking all exception silently. This method fails by throwing exception but you fail to see it (pun intended).

Please check it up by running under debugger step-by-step and/or handling the caught exception.

I understand this is only experimental, but in real-life development you should never catch exception like that, blocking its further propagation. Moreover, excessive use of try-catch defeats to purpose of exceptions. In most cases you need to catch exceptions on the very top of stack of each thread; also in the top-level UI cycle.

I would also advice to use System.Diagnostics.Debug.Assert for assertions.

—SA


Assert.Fail总是抛出AssertFailException.您的Catch块是空的,因此您正在捕捉和食用AssertFailExpection.由于Catch块中没有任何内容,因此发生故障时将不会发生任何事情.您的代码按预期工作.

如果要使测试失败,则必须重新抛出捕获的异常,或者只是在Catch块中放入一个Assert.Fail.
Assert.Fail always throws an AssertFailException. Your Catch block is empty, so you''re catching, and eating, the AssertFailExpection. Since there''s nothing in the Catch block, nothing is going to happen when the fail occurs. Your code is working as expected.

If you want the test to fail, you''ll either have to rethrow the exception you caught or just put an Assert.Fail inside the Catch block.


也许您错过了我的观点试图制造.我的意思是:

如果Assert.Fail语句在try块内,则即使代码执行执行assert.fail,测试也将始终通过.例如,即使子程序未能引发异常,以下测试也将始终通过.

Perhaps you missed the point that I was trying to make. My point is this:

If the Assert.Fail statement is within a try block, the test will ALWAYS pass, even if the code execution executes the assert.fail. For example, the following test will always pass, even if the sub fails to throw an exception.

<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中尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆