What is the difference between the SetupGet() and Setup() methods for MOQ?
Asked
Active
Viewed 2.1k times
2 Answers
68
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().

John Allers
- 3,052
- 2
- 30
- 36
-
I think we can use Setup for skipping the method specified. – setzamora May 21 '13 at 05:58
18
SetupGet works when you're trying to mock read only property

Anton
- 1,409
- 1
- 19
- 37
-
1@SarawutPositwinyu, I checked again, and you're right in sense that it won't work, but in case you're mocking properties of class and not of interface - you're doing something not in spirit of Dependency Inversion. And on interface - you just can't specify r-o property. – Anton May 28 '15 at 06:22