Friday, March 25, 2016

Mocking a private property setter in your Unit Tests using Moq

I recently encountered a requirement whereby I had to set the value for a property that had a private setter in my Mock objects.

Fortunately, Moq has a solution for this!

You can use the .SetupGet method to set a private property as follows:


myMock.SetupGet(m => m.MyPrivateSetter).Returns(myValue);

If you want to read more about how to accomplish things using Moq, you can read the QuickStart and API Docs here:

https://github.com/Moq/moq4/wiki/Quickstart

http://www.nudoq.org/#!/Packages/Moq/Moq/Mock(T)/M/SetupGet(TProperty)

No comments:

Post a Comment