Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should().Throw and Should().NotThrow throw for Func<T> #951

Merged
merged 14 commits into from Nov 8, 2018

Conversation

krajek
Copy link
Contributor

@krajek krajek commented Oct 19, 2018

Closes #422.

This is still work in progress but if you would like to do an early review I would appreciate.
Implemented according to @jnyrup idea of returning AndWhichConstraint from NotThrow which allows continued assertions like this:

//-----------------------------------------------------------------------------------------------------------
// Arrange
//-----------------------------------------------------------------------------------------------------------
Func<int> f = () => 12;
 //-----------------------------------------------------------------------------------------------------------
// Act / Assert
//-----------------------------------------------------------------------------------------------------------
f.Should().NotThrow().Which.Should().Be(12);

All the commented code and tests are part of the WIP.

Copy link
Member

@jnyrup jnyrup left a comment

Choose a reason for hiding this comment

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

To align with Action assertions we should add
public AndWhichConstraint<FunctionAssertions<T>, T> NotThrow<TException>
public ExceptionAssertions<TException> ThrowExactly<TException>

Src/FluentAssertions/Specialized/FunctionAssertions.cs Outdated Show resolved Hide resolved
Src/FluentAssertions/Specialized/FunctionAssertions.cs Outdated Show resolved Hide resolved
Src/FluentAssertions/Specialized/FunctionAssertions.cs Outdated Show resolved Hide resolved
Tests/Shared.Specs/FunctionExceptionAssertionSpecs.cs Outdated Show resolved Hide resolved
Tests/Shared.Specs/FunctionExceptionAssertionSpecs.cs Outdated Show resolved Hide resolved
Tests/Shared.Specs/FunctionExceptionAssertionSpecs.cs Outdated Show resolved Hide resolved
@krajek krajek changed the title [WIP] Should().Throw and Should_().NotThrow throw for Func<T> [WIP] Should().Throw and Should().NotThrow throw for Func<T> Oct 28, 2018
@krajek krajek changed the title [WIP] Should().Throw and Should().NotThrow throw for Func<T> Should().Throw and Should().NotThrow throw for Func<T> Oct 29, 2018
@krajek
Copy link
Contributor Author

krajek commented Oct 29, 2018

Ready for the merge with squash.

To align with Action assertions we should add
public AndWhichConstraint<FunctionAssertions, T> NotThrow

NotThrow<TException> does return void, because it does not guarantee that no exception was thrown and that assertion can be continued.

Copy link
Member

@jnyrup jnyrup left a comment

Choose a reason for hiding this comment

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

Looks good 👍

I didn't thought about the difficulty of the return type for NotThrow<TException>().
For now I agree with returning void.

At some later point I would like if we could come up with a nice way to represent the three possible outcomes:

  • didn't throw any exception => success and pass on result for chaining further assertions.
  • threw an exception but it was not the unexpected => success but no resulting value.
  • threw an exception and it was the unexpected one => fail.

@krajek krajek changed the title Should().Throw and Should().NotThrow throw for Func<T> [WIP] Should().Throw and Should().NotThrow throw for Func<T> Oct 29, 2018
@krajek
Copy link
Contributor Author

krajek commented Nov 2, 2018

@jnyrup tests you requested are added, it was a good idea.

Additionally, FunctionAssertions now inherits from ReferenceTypeAssertions. It was not extremely important to do it, but at least it is consistent with ActionAssertions among others.

Ready for a merge with squash.

@krajek krajek changed the title [WIP] Should().Throw and Should().NotThrow throw for Func<T> Should().Throw and Should().NotThrow throw for Func<T> Nov 2, 2018
@dennisdoomen dennisdoomen self-assigned this Nov 7, 2018
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.

None yet

3 participants