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

NUnit2025 fires unnecessarily #621

Closed
manfred-brands opened this issue Oct 27, 2023 · 3 comments · Fixed by #624
Closed

NUnit2025 fires unnecessarily #621

manfred-brands opened this issue Oct 27, 2023 · 3 comments · Fixed by #624
Assignees

Comments

@manfred-brands
Copy link
Member

manfred-brands commented Oct 27, 2023

Given:

var item1 = new object();
var item2 = "Hello";
var collection = new[] { item1, item2 };

Assert.That(collection, Does.Contain(item1));
Assert.That(collection, Does.Contain(item2));

NUnit2025 fires for the second line: The contains constraint cannot be used with an actual value of 'object[]'.
However the tests succeed in NUnit.

In the analyzer, the rule fires always if actual is neither a string nor an IEnumerable<string>.

The ContainsClassicModelAssertUsageCodeFix converts Assert.Contain into Does.Contain.
I made the latter because that was the code we used a lot in our projects.
As NUnit has multiple ways to do the same thing, maybe the it is the wrong method, but NUnit itself considers it ok,
hence so should the analyzer.

Note that Contains.Item has no equivalent for not, e.g. Contains.No.Item does not exist.
We do have Has.Member and Has.No.Member.

@manfred-brands
Copy link
Member Author

manfred-brands commented Oct 27, 2023

@mikkelbu Do you agree to disable the analyzer or should Does.Contain not be used for other collections?

@manfred-brands
Copy link
Member Author

There was an explicit commit to allow Does.Contain` for collections:

Author: Rob Prouse <rob@prouse.org>
Date: 26/08/2014 04:42:06
Commit hash: 88cbece5c5c40d4dccf368453cbc0f0d9fb043f2

Added collections to the Does.Contain syntax and cleaned up the various Contain/Contains methods to return the right type of Contraints for strings and collections.

@mikkelbu
Copy link
Member

@manfred-brands From what you have written above it sounds like the analyzer should be disabled

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

Successfully merging a pull request may close this issue.

2 participants