问题描述
我的代码对应该"正确的数据进行操作.但是在开发过程中,有时会获得无效数据.
发生这种情况时,我想提出调试断言,如果用户选择继续,则代码将过滤掉无效记录并继续对安全"数据进行操作.
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.
问题:有没有办法解决单元测试中的assert调用?
等同于单击断言失败"对话框中的OK=Continue
吗?
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
推荐答案
您不应为此使用Debug.Assert
.Debug.Assert
仅旨在用作调试辅助.
在发布模式下将完全不会编译它.
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.
相反,您应该创建自己的方法,该方法将向用户显示一个更简单的对话框,并且可以将其配置为始终继续进行单元测试. (例如,使用public static bool ShowWarnings
属性)
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)
这篇关于有没有办法从代码中的Debug.Assert()之后继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!