You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Walking through our codebase at work, I found out that 100% of ContainSingle(string) usages (written by different people) are wrong.
What people think when they write new[] { "a" }.Should().ContainSingle("b") is: we test that array only has a single element and it is "b".
People might also think that new[] { "a" }.Should().ContainSingle("b") means: we test that array contains only one "b", but could contain any number of other elements.
What new[] { "a" }.Should().ContainSingle("b")actually means is: we test that array only has a single element. If it doesn't, error message will say we expected only a single element "because b".
I don't have a good suggestion on how to fix ContainSingle(string), but it is clear for me that current method is not intuitive to use and leads to wrong tests.
Possibly ContainSingle(string) should just be deprecated in favor of already existing ContainSingle(predicate). So we write new[] { "a" }.Should().ContainSingle(e => e == "b")
Alternative Concerns
No response
Are you willing help with a pull-request?
No
The text was updated successfully, but these errors were encountered:
Actually, new[] { "a" }.Should().Equal("b") does the same thing. The only sensisble version of ContainSingle would consist of parameterless version to assert that the collection contains only a single item.
Background and motivation
Walking through our codebase at work, I found out that 100% of
ContainSingle(string)
usages (written by different people) are wrong.What people think when they write
new[] { "a" }.Should().ContainSingle("b")
is: we test that array only has a single element and it is"b"
.People might also think that
new[] { "a" }.Should().ContainSingle("b")
means: we test that array contains only one"b"
, but could contain any number of other elements.What
new[] { "a" }.Should().ContainSingle("b")
actually means is: we test that array only has a single element. If it doesn't, error message will say we expected only a single element "because b".I don't have a good suggestion on how to fix
ContainSingle(string)
, but it is clear for me that current method is not intuitive to use and leads to wrong tests.Possibly
ContainSingle(string)
should just be deprecated in favor of already existingContainSingle(predicate)
. So we writenew[] { "a" }.Should().ContainSingle(e => e == "b")
Alternative Concerns
No response
Are you willing help with a pull-request?
No
The text was updated successfully, but these errors were encountered: