Question

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?

Was it helpful?

Solution

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:

OTHER TIPS

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() );

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.

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". ;)

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