Skip to content

BeOfType does not attach to the AssertionScope #1002

@lg2de

Description

@lg2de

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions