NUnit: Why Doesn't Assert.Throws Catch My ArgumentNullException?

后端 未结 1 460
北恋
北恋 2020-12-15 20:27

I am posting this question anew at the behest of the distinguished Mr. John Skeet, who suggested I devise a simple test program that isolates and demonstrates the issue I am

相关标签:
1条回答
  • 2020-12-15 21:15

    What actually happens is that Assert.Throws does catch your exception, however Visual Studio stops on the first-chance exception anyway. You can check this by just pressing F5; Visual Studio will happily carry on executing.

    As the exception helper tells you, the exception was unhandled by user code. So we know that Visual Studio doesn’t consider NUnit to be user code for some reason.

    enter image description here

    Visual Studio actually tells you this in plain text, if you know where to look:

    enter image description here

    There is also evidence of this fact in the stack trace:

    enter image description here

    Solution 1: Use a debug build of NUnit with debugging symbols. That will get Visual Studio to regard NUnit as user code, and thus stop treating your exceptions as "unhandled by user code". This isn’t trivial, but might work better in the long term.

    Solution 2: Turn off the "Enable Just My Code" checkbox in Visual Studio’s debugging settings:

    enter image description here

    P.S. I’m not considering work-arounds whereby you avoid the use of Assert.Throws<T> altogether, but there are of course ways of doing that.

    0 讨论(0)
提交回复
热议问题