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

ContainSingle(string) is confusing #2512

Open
slonopotamus opened this issue Dec 13, 2023 · 2 comments
Open

ContainSingle(string) is confusing #2512

slonopotamus opened this issue Dec 13, 2023 · 2 comments

Comments

@slonopotamus
Copy link

slonopotamus commented Dec 13, 2023

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 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

@dennisdoomen
Copy link
Member

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.

@dennisdoomen
Copy link
Member

Actually, that doesn't make sense either. You can already call ContainSingle without parameters.

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

No branches or pull requests

2 participants