System.Diagnostics.Debugger.Break(); // this DB call has not been mocked
I'd like to place these in code at the site of I/O calls to let unit test developers know when they haven't properly mocked out the call, but I don't want to create new problems.
Does calling Debugger.Break() have any impact when compiled as Release?
Does it have any impact when executing a Debug build but not debugging?
Unfortunately for your use-case, it does.
To address your concerns:
Tim Schmelter had a good suggestion. If you want to limit Debugger.Break to only Debug builds then you can use Conditional Compilation .
#if DEBUG
Debugger.Break();
#endif
If there's no debugger attached then it'll either try to attach a debugger, or send a message to the Windows Error Reporting (WER) subsystem depending on what version of the .Net Framework you're targeting.
If no debugger is attached, users are asked if they want to attach a debugger.
...
Starting with .NET Framework 4, the runtime no longer exercises tight control of launching the debugger for the Break method, but instead reports an error to the Windows Error Reporting (WER) subsystem.
As the comments and @AndyJ's answer explain, your program will crash if no debugger is attached to the process.
What you may want is to check if the debugger is attached before calling Debug.Break
at runtime. There is an API property for that, System.Diagnostics.Debugger.IsAttached
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.