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

BeOfType does not attach to the AssertionScope #1002

Closed
lg2de opened this issue Mar 1, 2019 · 1 comment
Closed

BeOfType does not attach to the AssertionScope #1002

lg2de opened this issue Mar 1, 2019 · 1 comment
Labels

Comments

@lg2de
Copy link
Contributor

@lg2de lg2de commented Mar 1, 2019

Description

BeOfType seems not to (fully) support AssertionScope.
Failed assertion will immediately cause to exit the scope.
I think it is related to direct cast of the subject to the expected type which may throw an InvalidCastException.

Complete minimal example reproducing the issue

var x = string.Empty;
using (new AssertionScope())
{
    x.Should().Be("Z");
    x.Should().BeOfType<long>();
    x.Should().BeOfType<int>();
}

Expected output

Expected x to be "Z" with a length of 1, but "" has a length of 0, differs near "" (index 0).
Expected type to be System.Int64, but found System.String.
Expected type to be System.Int32, but found System.String.

bei FluentAssertions.Execution.LateBoundTestFramework.Throw(String message) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\LateBoundTestFramework.cs:Zeile 13.
bei FluentAssertions.Execution.TestFrameworkProvider.Throw(String message) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\TestFrameworkProvider.cs:Zeile 40.
bei FluentAssertions.Execution.CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\CollectingAssertionStrategy.cs:Zeile 41.
bei FluentAssertions.Execution.AssertionScope.Dispose() in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\AssertionScope.cs:Zeile 265.
bei ValidateType() in Tests.cs:Zeile XXX.

Actual output

Expected x to be "Z" with a length of 1, but "" has a length of 0, differs near "" (index 0).
Expected type to be System.Int64, but found System.String.

bei FluentAssertions.Execution.LateBoundTestFramework.Throw(String message) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\LateBoundTestFramework.cs:Zeile 13.
bei FluentAssertions.Execution.TestFrameworkProvider.Throw(String message) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\TestFrameworkProvider.cs:Zeile 40.
bei FluentAssertions.Execution.CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context) in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\CollectingAssertionStrategy.cs:Zeile 41.
bei FluentAssertions.Execution.AssertionScope.Dispose() in C:\projects\fluentassertions-vf06b\Src\FluentAssertions\Execution\AssertionScope.cs:Zeile 265.
bei ValidateType() in Tests.cs:Zeile XXX.

Versions

Tested with FluentAssertions 5.5.3 and 5.6.0 targeting .NET framework 4.7.2

Additional Information

I think, BeOfType should use safe cast instead of direct cast.
Maybe like this

public AndWhichConstraint<TAssertions, T> BeOfType<T>(string because = "", params object[] becauseArgs)
{
    BeOfType(typeof(T), because, becauseArgs);
    return new AndWhichConstraint<TAssertions, T>((TAssertions)this, Subject as T);
}

I you agree I can create such a pull request.

@jnyrup
Copy link
Member

@jnyrup jnyrup commented Mar 1, 2019

Nice catch @lg2de

It looks similar to #838, so you're very welcome to create a PR to fix this 👍

By quickly skimming the code, it looks like BeAssignableTo suffers the same problem?
If you'd like, feel free to include a fix for that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants