Auto-mocking extensions should auto-fill properties



dhilgarth wrote Sep 8, 2011 at 3:00 PM

As I commented on the blog, it might be a good idea to let the user choose whether he want to have the properties of his mocks filled or not. Why? Because at least for Rhino Mocks and Moq it makes a difference, as they support auto property behavior or null behavior (values assigned to properties are lost, properties always return default(T))

baxevanis wrote Sep 8, 2011 at 8:26 PM

That would be nice. For example, in order to mock an IController interface and fill the User property one needs to create mocks for IPrincipal, HttpContextBase and ControllerContext then assign the User to HttpContextBase, assign that to ControllerContext and finally assign to mocked controller instance.

This overall process will by done automatically, I think, when this feature is implemented. So this would be a very nice extension.

dhilgarth wrote Sep 9, 2011 at 6:23 AM

Please have a look at the AutoNSubstitute fork. In this I implemented the auto-filling functionality for mocks created with NSubstitute. Does it behave the way you expected?

ploeh wrote Sep 9, 2011 at 7:35 AM

While I agree that this would make it easier to work with HttpContextBase and its ilk, keep in mind that this basically demonstrates how crappy an API the web abstractions actually are ;)

baxevanis wrote Sep 9, 2011 at 7:53 AM

...Indeed :)

vinneyk wrote Apr 3, 2013 at 2:55 PM

My group has adopted the practice of exposing only interfaces as return objects and parameters for our business-layer API. This is a fairly new change so it's possible that we'll eventually come to understand why Mark considers interface properties to be a code smell. However, for the time being, this has allotted the following benefits for our MVC application: (1) ViewModel objects can implement parameter interfaces which prevents the need to convert from the vm to the service model and (2) views models tend to be composed of one or more interfaces rather than deriving, exposing, or duplicating the properties of the service model.

So far, the two areas where this practice has been difficult are the lack of support for interface parameters in the MVC WebAPI actions and this inability to create anonymous mocks for testing. If this feature is indeed not desired by the AutoFixture team, perhaps someone from the team could give some further clues as to how to extend this functionality ourselves.

Thanks for the awesome AutoFixture libraries!

ploeh wrote Apr 8, 2013 at 5:59 AM

FWIW, I've posted a couple of blog posts describing how to achieve this effect: http://blog.ploeh.dk/2013/04/08/how-to-automatically-populate-properties-with-automoq/

mfreidge wrote Jun 18, 2013 at 12:33 PM

@vinneyk, Do not overuse interfaces - see


From http://martinfowler.com/bliki/InterfaceImplementationPair.html

Using interfaces when you aren't going to have multiple implementations is extra effort to keep everything in sync.Furthermore it hides the cases where you actually do provide multiple implementations.