Is there any way to continue after Debug.Assert () from the code?

My code works with data that "should" be correct. However, during development, there are times when I receive incorrect data.
When this happens, I would like to raise the debugging argument, and if the user decides to continue, the code will filter out invalid entries and continue to work with "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 a unit test code path when I decided to work with filtered data.

Question: Is there a way to overcome the assert call in unit test?
Is some equivalent to clicking OK=Continue in the Confirmation Error dialog box?

TIA

+4
source share
2 answers

You should not use Debug.Assert for this.
Debug.Assert is for use as a debugging tool only.
It will not compile at all in Release mode.

Instead, you should create your own method that shows the user a simpler dialog box and can be configured to always continue for unit testing. (e.g. use the public static bool ShowWarnings )

+6
source

In addition to the SLaks answer, I would add that what you want to do is logically inconsistent. The statement should be used to document the condition under which it cannot be false. If a false condition arises, you know that you have a mistake; the purpose of the statement is (1) as a kind of commentary that describes to the reader what should be true at that point in the code, and (2) a debugging aid that tells you when you have an error.

Since the correct statements in the correct code never work, there is no way to test confident shelling. The premise of the test is that it creates a possible configuration for your software and verifies that it is correct; but the correct code with the correct statements never has a configuration in which assert fires.

You seem to be using Assert to not document something that you know is true, but rather what you hope is true or usually true. Do not use for this statement. If there are any program entries that cause a violation of the statement, you must either delete this statement or throw an exception when you receive invalid data so that this statement never sees. Statements are intended to document what should be true, not what is true most of the time.

See also this related question:

Debug.Assert vs Exception Throwing

+10
source

Source: https://habr.com/ru/post/1338256/


All Articles