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

1

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!

Karikaria answered 23/12, 2011 at 9:24 Comment(3)
Why do you want to mock an IList? Just create a List<T> and use that. Is there some behavior of the IList you're looking to test?Spicer
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'?Nuptial
I am referring to this -> code.google.com/p/moq-contrib/wiki/Automocking. Trying to mock a containerKarikaria
I
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.

Inspiratory answered 23/12, 2011 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.