FrenkyB FrenkyB - 1 month ago 23
C# Question

Compare collection with mock collection

I am unit testing one of my functionalities. Results are OK, but collections of result from method are of type

Position
, collection for comparison is of type
Mock<IPosition>
. That is why test always fails, because comparison between the two is not correct.

Is there a way I can override default comparison between collections?

Results are of type Position, which implements this interface:

public interface IPosition
{
char HorizontalPosition { get;}
int VerticalPosition { get; }
}


But mock collection is of type
Mock<IPosition>
.

Answer

You have to modify the collections:

var positions = new List<Position>();
var mocks = new List<Mock<IPosition>>();

YourComparisonMethod(positions.Cast<IPosition>(), mocks.Select(m => m.Object));