xunit icon indicating copy to clipboard operation
xunit copied to clipboard

Assert.Throws<ArgumentException>(paramName:null, Action) throws ArgumentNullException

Open ericstj opened this issue 4 years ago • 2 comments

Regression introduced here: https://github.com/xunit/assert.xunit/commit/726e48276895ecba39468990809664948113d876#diff-829bc159adac0f20073a5979fa4b4db6d7586a249058328199b486cf26b01070R189

Observed in xUnit 2.4.2-pre.13

For better or worse, we have framework code that doesn't specify a parameter name for some ArgumentExceptions. EG: https://github.com/dotnet/runtime/blob/57bfe474518ab5b7cfe6bf7424a79ce3af9d6657/src/libraries/System.Drawing.Common/tests/FontFamilyTests.cs#L63-L64

Specifying null for parameterName used to work for testing this scenario but now it throws in the Assert library. Can we bring back the old behavior?

ericstj avatar Oct 06 '21 01:10 ericstj

I'll add that we can workaround this by using Assert.Throws<ArgumentException>(Action) but we'd actually like to Assert a null parameter name, since it's clear that we didn't forget to test it. Today we'll use our own wrapper to do this, but I wanted to raise this breaking change of behavior.

ericstj avatar Oct 07 '21 16:10 ericstj

This was probably just overly zealous on my part when I went through and introduced all the nullability. I probably assumed paramName was required in the exception.

bradwilson avatar Oct 16 '21 17:10 bradwilson

This is breaking tests in https://github.com/dotnet/winforms/ after we've been forced to update to 2.4.2....

RussKie avatar Aug 26 '22 07:08 RussKie

The workaround is to not use the Assert.Throws overload that checks the param name, and instead just use the normal Assert.Throws, take the return value (which is the exception) and do a separate Assert.Null.

bradwilson avatar Aug 27 '22 00:08 bradwilson

Thank you for the guidance.

RussKie avatar Aug 29 '22 06:08 RussKie

FYI, this will be fixed in 2.5.0.

bradwilson avatar May 06 '23 23:05 bradwilson