Moq: Working With SetupGet, VerifyGet, SetupSet, VerifySet, SetupProperty

Moq has a set of methods specific to stubbing and verification of properties. In this post I’m going to go over each one of them, and explain when and why to use them. I also discuss their differences and in what situation we might want to use them. Let’s assume we have an interface with couple of properties, a class that implement that interface, and a class that consumes it. In the subsequent sections, I’ll use these classes as example to show you how to use Moq to stub and verify properties.

Moq SetupGet

SetupGet is the simplest one in my opinion. It helps us stub the property with certain value to see if our underlying class passes that value back to us correctly.

Moq VerifyGet

VerifyGet helps us verify that property’s getter accessed at least a number of times or not at all. Here we call GetName and then verify that it correctly calls the getter of FirstName property.

Moq SetupSet

SetupSet helps us set expectation for our setters, that is we expect our setter to be set with specific value. Here we say that we expect that FirstName will be set with “Knights Of Ni!” value. We also declare it as Verifiable so we can verify it in the end. After that we call the ChangeName method and tell it to change the name of our property through its setter. Finally, we verify the expectation that we setup earlier.

Moq VerifySet

With VerifySet we can take a more direct approach in verifying that the property on our mock object is set with our value. Here we change the name with ChangeName method, and then verify that FirstName property is indeed set with that value.

Verifying Method Pass The Correct Argument

Sometimes we want to make sure that the consumer of some class passes an argument correctly to the method that it uses. In these situations we can verify that a method on the mock object is called with specific arguments. We can also check how many times that method is called.

Moq SetUpProperty

SetUpProperty tell our mock object to start tracking that property. In this contrived example we track our property and then change its value. In the end we assert that our property has the value we set for it. Notice that if we don’t use SetupProperty the assertion will fail, because the mock doesn’t track the property value. We also can set initial value for our property and then track it, as you can see it the gist above.

Difference Between SetupGet And SetupProperty


When we stub our property with SetupGet, it stubs the property and after that our property is read only. Any other change to that property isn’t tracked. With SetupProperty on the other hand, we can set the initial value, and if we expect our value to change along the way we can track that too.

Moq SetupAllProperties

If SetupProperty only track one specific property, SetupAllProperties track all the properties on our mock object. In this example if we comment out SetupAllProperties and uncomment  SetupProperty the assertions will fail. Because we only track the FirstName and not the last name. if we don’t want to track all of them we should track our properties one by one.

Summary

In this post, I’ve explained how Moq can help us  stub, test and verify the value of our properties. I also discussed the differences between SetupGet and SetupProperty and how we can make sure that correct argument is passed to our methods. In case you wanted to experiment with above examples, you can download the example project from here.

Share...
  • 10
    Shares
 

Hamid Mosalla

Hi, I'm Hamid Mosalla, I'm a software developer, indie cinema fan and a classical music aficionado. Here I write about my experiences mostly related to web development and .Net.

 

Leave a Reply

Your email address will not be published. Required fields are marked *