Setup() vs SetupGet()

Moq

Moq Problem Overview


What is the difference between the SetupGet() and Setup() methods for MOQ?

Moq Solutions


Solution 1 - Moq

Setup() can be used for mocking a method or a property.

SetupGet() is specifically for mocking the getter of a property. Took a quick peek at the Moq source code and it looks like if you use Setup() on a property getter, it will call SetupGet(). So in that case, it is probably more personal preference as to whether you want to be more explicit and use SetupGet() instead of Setup().

Of course, my knowledge of Moq is limited, so I don't know if there special cases where you would need to use SetupGet() over Setup().

Solution 2 - Moq

SetupGet works when you're trying to mock read only property

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNil PunView Question on Stackoverflow
Solution 1 - MoqJohn AllersView Answer on Stackoverflow
Solution 2 - MoqAntonView Answer on Stackoverflow