Skip to content

Inconsistency between StringAssertions.StartWith and String.StartsWith #1410

@Overlong

Description

@Overlong

Description

StringAssertions.StartWith throws an ArgumentException when asserting that a string starts with the empty string.

This makes it so that the assertions in one of the tips are not equivalent when expectedPrefix is empty.

Complete minimal example reproducing the issue

string actual = "abc";
string expectedPrefix = "";

actual.StartsWith(expectedPrefix).Should().BeTrue();
actual.Should().StartWith(expectedPrefix);

Expected behavior:

Both assertions to pass.

Actual behavior:

The first assertion passes, but the second throws an ArgumentException with message

Cannot compare start of string with empty string. (Parameter 'expected')

Versions

  • Fluent Assertions: 5.10.3
  • .NET runtime: .NET Core 3.1

Additional Information

StringAssertions.EndWith and String.EndsWith are inconsistent in the same way.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions