5

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!

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
zhengtonic
  • 720
  • 12
  • 25
  • 5
    Why do you want to mock an IList? Just create a List and use that. Is there some behavior of the IList you're looking to test? – PatrickSteele Dec 23 '11 at 13:56
  • It isn't clear from your question what you are having trouble achieving. There may be valid reasons to mock an IList - but what do you mean by 'automock'? – Tim Long Dec 24 '11 at 01:06
  • I am referring to this -> http://code.google.com/p/moq-contrib/wiki/Automocking. Trying to mock a container – zhengtonic Jan 11 '12 at 08:54

1 Answers1

8

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.

Gilles
  • 5,269
  • 4
  • 34
  • 66