Skip to content

Add WithParameterName#1466

Merged
jnyrup merged 1 commit intofluentassertions:developfrom
jnyrup:WithParameterName
Jan 11, 2021
Merged

Add WithParameterName#1466
jnyrup merged 1 commit intofluentassertions:developfrom
jnyrup:WithParameterName

Conversation

@jnyrup
Copy link
Copy Markdown
Member

@jnyrup jnyrup commented Jan 10, 2021

Add WithParameterName extension to assert on the ParamName property on ArgumentException.

This fixes #1453

@jnyrup jnyrup merged commit d641951 into fluentassertions:develop Jan 11, 2021
@jnyrup jnyrup deleted the WithParameterName branch January 11, 2021 06:44
/// </param>
public static ExceptionAssertions<TException> WithParameterName<TException>(
this ExceptionAssertions<TException> parent,
string paramName,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this has already been merged, but I'd suggest adding argument validation (!= null) for paramName here to improve the stacktrace in case people misuse the method.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think about that case, but ArgumentException allows a null ParamName.

Maybe I'm misunderstanding you, can you give an example on "misuse"?

params object[] becauseArgs)
where TException : ArgumentException
{
parent.Which.ParamName.Should().Be(paramName, because, becauseArgs);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: won't the final error message here be a bit cryptic to an outside consumer? Wouldn't it be better to create a custom validation instead of relying on the standard Should().Be in this case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll change that.

/// <param name="becauseArgs">
/// Zero or more objects to format using the placeholders in <paramref name="because"/>.
/// </param>
public static async Task<ExceptionAssertions<TException>> WithParameterName<TException>(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this one have the Async suffix?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current approach in this class is that they don't have Async suffix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Request] Add 'WithParamName' validation extension for 'ArgumentException' and derived exception types

3 participants