views:

239

answers:

4

Consider the following:

new SUT(null, null, new List<IObjectBeingMocked>() { mockObjectOne.Object, mockObjectTwo.Object })

My SUT (System Under Test) needs a list of objects as the third parameter. These need to be mocks as I've set some expectatioins on these.

How would I clear it up so that I can remove the need to call .Object on each item in the list? There are only two items usually but this could grow and in my opinion this makes the test harder to read.

What would be the best way of transforming this list of mock objects into actual objects easily/nicely?

+1  A: 

Perhaps you could use extension methods to manage the list creation and invocation of the Object property for you.

var list = new List<Mock<Foo>>() { ... };
new SUT( null, null, list.Select( o => o.Object ).ToList() );
tvanfosson
A: 

You could introduce explaining variables (locals) for clarity:

IObjectBeingMocked objectOne = mockObjectOne.Object;
IObjectBeingMocked objectTwo = mockObjectTwo.Object;

new SUT(null, null, new List<IObjectBeingMocked>() { objectOne, objectTwo });

EDIT: Perhaps with better-chosen names than my "objectOne", "objectTwo". ;)

TrueWill
A: 

A good way to handle this is to create a GetSUT() method that creates it only once in one place. Then as your SUT creation changes, you only have to change it in one place.

Chris Missal
+1  A: 

You can use Mocks.Query() method which will return a collection of all mocked SUT instances.
This feature exists in the beta version of moq. See the details here:

Ohad Horesh
This is true hence why I accepted it but my version of Moq does not support it as I'm not using the beta version. Its something I will look into though.
Finglas