有没有办法在代码中的 Debug.Assert() 之后继续?
我的代码对“应该”正确的数据进行操作。然而在开发过程中,有时我会获取无效数据。
当发生这种情况时,我想引发调试断言,如果用户选择继续,代码将过滤掉无效记录并继续对“安全”数据进行操作。
// assert incorrect data
Debug.Assert(person.Items.All(item => item.IsValid), "Inconsistent data!");
// operate on filtered data
this.ItemViewModels = new ObservableCollection<ItemViewModel>(
person.Items
.Where(i =>item.IsValid) // Use only correct data
.Select(i => new ItemViewModel(lang, i)));
当我选择对过滤后的数据进行操作时,我想对代码路径进行单元测试。
问题:有没有办法通过单元测试中的断言调用?
相当于在“断言失败”对话框中单击OK=Continue
?
TIA
My code operates on data which "should" be correct. However during development there are occasions when I obtain invalid data.
When that happens I would like to raise the debug assert and, if user choose to continue, the code would filter out the invalid records and continue to operate on "safe" data.
// assert incorrect data
Debug.Assert(person.Items.All(item => item.IsValid), "Inconsistent data!");
// operate on filtered data
this.ItemViewModels = new ObservableCollection<ItemViewModel>(
person.Items
.Where(i =>item.IsValid) // Use only correct data
.Select(i => new ItemViewModel(lang, i)));
I would like to unit test the code path when I choose to operate on the filtered data.
Question: Is there a way how to get past the assert call in the unit test?
Some equivalent to clicking OK=Continue
in the "Assertion Failed" dialogue?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了 SLAks 的回答之外,我还要补充一点,您想要做的事情在逻辑上是不一致的。应该使用断言来记录不可能为假的条件。如果出现错误条件,那么您就知道存在错误;断言的目的是(1)作为一种注释,向读者描述代码中此时必须正确的内容,以及(2)作为调试辅助工具,告诉您何时出现错误。
由于正确代码中的正确断言永远不会触发,因此无法测试断言触发。测试的前提是它产生您的软件的可能配置并验证其正确性;但具有正确断言的正确代码永远不会有触发断言的配置。
听起来您使用 Assert 不是为了记录您知道的内容,而是记录您希望为真的内容或通常为真的内容。不要为此使用断言。如果程序中有任何输入导致断言被违反,那么您需要删除断言,或者在获取无效数据时引发异常,以便断言永远不会看到它。断言旨在记录必须为真的内容,而不是大多数情况下为真的内容。
另请参阅此相关问题:
Debug.Assert 与异常抛出
In addition to SLaks's answer I would add that what you want to do is logically inconsistent. An assert should be used to document a condition that cannot possibly be false. If the false condition ever arises then you know you have a bug; the purpose of an assert is (1) as a kind of comment that describes to the reader what must be true at this point in the code, and (2) a debugging aid that tells you when you have a bug.
Since correct asserts in correct code never fire, there is no way to test an assert firing. The premise of a test is that it produces a possible configuratin of your software and verifies its correctness; but correct code with correct asserts never have a configuration where the assert fires.
It sounds like you are using Assert not to document something that you know is true but rather something that you hope is true or is normally true. Don't use assertions for that. If there is any inputs to the program that cause the assertion to be violated then you need to either remove the assertion, or cause an exception when you get the invalid data so that the assertion never sees it. Assertions are meant to document what must be true, not what is true most of the time.
See also this related question:
Debug.Assert vs Exception Throwing
您不应为此使用
Debug.Assert
。Debug.Assert
仅用作调试辅助。在Release模式下根本不会被编译。
相反,您应该创建自己的方法,它将向用户显示一个更简单的对话框,并且可以配置为始终继续进行单元测试。 (例如,使用
public static bool ShowWarnings
属性)You should not use
Debug.Assert
for this.Debug.Assert
is intended to be used only as a debugging aid.It will not be compiled at all in Release mode.
Instead, you should create your own method, which will show the user a simpler dialog box, and can be configured to always continue for unit testing. (eg, use a
public static bool ShowWarnings
property)