0

I have a list with offerIds which I pass to Moq setup. I am sure that in acting method execution I am calling this method with the same list but it returns null. My question is can Moq recognize ref objects f.e. List as the same Setup params?

_userOrderRepositoryMock
                .Setup(x => x.GetOrderIdsByOfferIds(offerIds))
                .ReturnsAsync(new Dictionary<Guid, Guid>() { });

Deivydas Voroneckis
  • 1,973
  • 3
  • 19
  • 40

1 Answers1

2

Moq will simply do a equality compare when used like you do above. A List<T> is a reference type so two lists will never equal each other, even if they contain similar contents. To get this to work I'd suggest you need to actually use It. So something like:

_userOrderRepositoryMock
                .Setup(x => x.GetOrderIdsByOfferIds(
                    It.Is<List<obj>>(i => i.All(offerIds))
                 ))
                .ReturnsAsync(new Dictionary<Guid, Guid>() { });

even this though depends on what offerIds is. If offerIds is a value type then this will work, if it's not then you will need to write some LINQ or an Equality function to define how you want these to perform equality on these objects.

See What is “Best Practice” For Comparing Two Instances of a Reference Type? for more information on this kind of thing.

Liam
  • 27,717
  • 28
  • 128
  • 190