List of Mock Objects (Moq) - Best Practices / Simplification

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. They should be mocking as I set some expectations on them.

How can I clear it to remove the need to call .Objectfor each item in the list? Usually there are only two elements, but this can grow, and in my opinion, this makes reading the test difficult.

What would be the best way to easily transform this list of mock objects into real objects?

+3
source share
4 answers

Mocks.Query(), SUT.
- moq. . :

+1

, Object .

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

GetSUT(), . , SUT , .

+2

(locals) :

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

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

: , , "objectOne", "objectTwo".;)

+1

Source: https://habr.com/ru/post/1717769/


All Articles