-
Notifications
You must be signed in to change notification settings - Fork 731
Description
Description
I'd like to have a WithParamName(string) (or similarly named) validation extension whenever an ArgumentException or any of its derived types is used in a validation.
Currently, we only have a WithMessage(string) extension.
In all my unit tests of public APIs, I always have tests checking that the expected argument exceptions are thrown, and that they are thrown for the correct arguments. This is important for methods where multiple arguments are present and more than one of them cannot be null. This requires me to write verbose code to check for the argument name. I believe an extension would make the code a bit simpler for this common case.
Expected behavior:
performingAction.Should().Throw<ArgumentNullException>().WithParamName("parameter");This should work seamlessly with other methods like WithMessage:
performingAction.Should().Throw<ArgumentNullException>()
.WithParamName("parameter")
.WithMessage("my exception message");Actual behavior:
performingAction.Should().Throw<ArgumentNullException>().Which.ParamName.Should().Be("parameter");Versions
-
Which version of Fluent Assertions are you using?
5.10.3 -
Which .NET runtime and version are you targeting?
Net 5.0
Additional Information
Alternatively, WithMessage could be dropped in favor of a different syntax, like:
performingAction.Should().Throw<ArgumentNullException>().Which.Should()
.TargetParameter("parameter")
.And
.HaveMessage("my exception message");This is more verbose, but probably considerably easier to extend (but has the potentially weird 2 Should calls).