0

I need method CreateAsync to return what it receives (the same List<enteties>)

Task<IEnumerable<Entity>> CreateAsync(IEnumerable<Entity> entities, CancellationToken? token = null);

I'm trying to mock a repository's method like that:

repository.Setup(i => i.CreateAsync(It.IsAny<List<enteties>>(), It.IsAny<CancellationToken>()))
            .ReturnsAsync((List<enteties> x, object y) => x);

But it returns empty IEnumareble

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
boom325
  • 11
  • It is up to you to provide mocked values. You know what list goes into CreateAsync, re-use the same list in ReturnAsync. – rene Apr 18 '23 at 12:05
  • Could you please provide [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Peter Csala Apr 18 '23 at 14:33
  • An uninitialized MOQ will return `null`/`default`. So if your setup is returning an empty `IEnumerable` then you must be passing it one. – Good Night Nerd Pride Apr 18 '23 at 14:35
  • Does this answer your question? [Returning value that was passed into a method](https://stackoverflow.com/questions/996602/returning-value-that-was-passed-into-a-method) – Selmir Aljic Apr 18 '23 at 16:32

1 Answers1

0

Moq can do what you want here and give back the same list it was given. Your only problem is the type mismatch in the argument matchers.

The interface uses CancellationToken? but the Setup in your question uses It.IsAny<CancellationToken>().

Change it to It.IsAny<CancellationToken?>() and it works:

repository.Setup(i => i.CreateAsync(It.IsAny<List<Entity>>(), It.IsAny<CancellationToken?>()))
            .ReturnsAsync((List<Entity> x, object y) => x);
dlumpp
  • 891
  • 8
  • 14