Some projects cannot be tested ... especially those that do not have the ability to test as one of the design goals. Typically, TDDed projects do not fall into this category.
To answer your original question, I posted an answer that includes using reflection in a slot in the requested error code. However, this may not work in all situations and is not a general solution.
The trade-off is to make an effort to write the test, given that this piece of code is under automatic tests. If you think that the cost-benefit ratio is huge, and the probability of failure is minimal, you can write it as an exceptional manual test, comment to future developers and check it manually. I would say that this is pragmatic, if you spent 30-40 minutes on a couple of brain attempts by the developers trying to get him to pass the test, maybe you need to step back and review your strategy. Take a look at Michael Peer, βWork Effectively With Legacy Code,β for some suggestions to overcome the barriers to verification.
source share