Xunit: Convert Assert.Collection to Assert.Single when there is only one lambda

Created on 12 Aug 2017  路  1Comment  路  Source: xunit/xunit

At https://github.com/xunit/xunit/issues/1388#issuecomment-320129318, @bradwilson suggested we could convert code like this:

Assert.Collection(items, item => AssertSomethingAbout(item));

to

var item = Assert.Single(items);
AssertSomethingAbout(item);

/cc @marcind, I see a lot of this in your codebase. Do you think this would be a good fix?

I was for this at first, but I think it could become a nuisance if it's likely the length of items will change in the future. Also, inlining lambdas can run into complications when they contain control flow statements, as I described here. It may also cause naming conflicts, e.g.

Assert.Collection(items1, item => ...);
Assert.Collection(items2, item => ...);

// =>

var item = Assert.Single(items1);
...
var item = Assert.Single(item2); // 'item' was already declared
Analyzers Discussion

Most helpful comment

I'm not sure this needs to be an analyzer (i.e. flagged as an issue), rather it could be 2 refactorings: converting from Assert.Single to Assert.Collection and the reverse. Esp. when you start with Assert.Single and the project evolves you might end up with more items in the collection so tooling help to rewrite the test would be nice.
Personally I use the Assert.Single variant because I prefer less indentation in the code.

>All comments

I'm not sure this needs to be an analyzer (i.e. flagged as an issue), rather it could be 2 refactorings: converting from Assert.Single to Assert.Collection and the reverse. Esp. when you start with Assert.Single and the project evolves you might end up with more items in the collection so tooling help to rewrite the test would be nice.
Personally I use the Assert.Single variant because I prefer less indentation in the code.

Was this page helpful?
0 / 5 - 0 ratings