Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup() vs SetupGet()

Tags:

moq

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

like image 640
Nil Pun Avatar asked Apr 23 '11 23:04

Nil Pun


People also ask

What does MOQ setup do?

'Setup' mocks a method and 'Returns' specify what the mocked method should return. 'Verifiable' marks this expectation to verified at the end when Verify or VerifyAll is called i.e. whether AddIncomePeriod was called with an object of IncomePeriod and if it returned the same output.

How do you set a mock object property?

You can set the mock object property to the value returned by the mock object method. To achieve this, specify separate behaviors for the method and the property. You can specify one behavior at a time. For more information about mock object behavior, see Specify Mock Object Behavior.

What is callback in MOQ?

Callbacks. A powerful capability of Moq is to attach custom code to configured methods and properties' getters and setters. This capability is often referred to as Callbacks.

What is MOQ mock?

Moq is a mocking framework built to facilitate the testing of components with dependencies. As shown earlier, dealing with dependencies could be cumbersome because it requires the creation of test doubles like fakes. Moq makes the creation of fakes redundant by using dynamically generated types.


2 Answers

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

like image 100
John Allers Avatar answered Sep 20 '22 14:09

John Allers


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

like image 31
Anton Avatar answered Sep 17 '22 14:09

Anton