Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to auto-mock a container (e.g. IList) in MOQ without extensions/contrib

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!

like image 465
zhengtonic Avatar asked Dec 23 '11 09:12

zhengtonic


1 Answers

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.

like image 195
Gilles Avatar answered Sep 22 '22 11:09

Gilles