Posted by: ronniebarker | March 24, 2009

Stubs in Moq

I had a discussion today regarding Moq and how to make a quick stub of a property so that a mock object could be thrown into somewhere and have the properties work as simple properties. The simple answer weas to use an expectation:

image

But that felt ugly – and not as useful as it could be.

After playing around for a while I found the Moq.Stub namespace. After placing a using to this namespace at the top you can do something along the lines of personMock.Stub( p => p.Name ):

image

or to mock out all of the properties use personMock.StubAll():

image

As you can see – if no expectation is needed stubbing properties is quite simple in Moq.


Leave a response

Your response:

Categories