Question

i wonder if it is possible to auto mock a container in MOQ without any additions to the MOQ lib. I am having problems finding a clean way to automock an IList.

Thanks in advance!

Was it helpful?

Solution

Answer to your question: No.

Do you really need to mock IList?

Mocks are typically used to:

  • To test behaviour (via expectations) rather than results.
  • To abstract away complex or heavy dependencies.
  • To simplify your tests code by easily returning a desired value.
  • To test only your class under tests.

You could for example mock a repository that access a database. Normally your tests would not mock a list but rather have a mocked object return a list with the data that you need for your test.

ie:

var aList = new List<int>() { 1, 2, 3, 4, 5 };
var mockService = new Mock<IMyService>();
mockService.Setup(mock => mock.GetFooList()).Returns(aList);

It might help clarify your question if you specify why you need to mock a container.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top