StructureMap's AutoMocking feature is one of my favorite Unit Testing Tools. Consider that you are writing tests and your SUT has two dependencies: one you care about and one that you don't (e.g. logging). Normally, you would need to still create a mock of your logger so that you could satisfy the constuctor. Not with AutoMocking. Consider the following base class:
publicabstractclassForSut<TSut>whereTSut:class{protectedForSut(){Mocker=newMoqAutoMocker<TSut>();}publicMoqAutoMocker<TSut>Mocker{get;}publicTSutSut=>Mocker.ClassUnderTest;publicMock<TInterface>GetMockFor<TInterface>()whereTInterface:class{returnMock.Get(Mocker.Get<TInterface>());}}The property Mocker is an instance of MoqAutoMocker, where TSut is the System Under Test that we are testing. Here is an example of the Sut:
publicclassSystemUnderTest{privatereadonlyISomeDependency_someDependency;publicSystemUnderTest(ISomeDependencysomeDependency){_someDependency=someDependency;}publicvoidDoWork(){_someDependency.LoadData();}}Nothing complicated here - just a single method that delegates to some dependency that will load data.
Using the base ForSpec we can create the xUnit Test:
publicclassUnitTest1:ForSut<SystemUnderTest>{[Fact]publicvoidTest1(){// ArrangevarmockDependecy=GetMockFor<ISomeDependency>();mockDependecy.Setup(x =>x.LoadData());// ActSut.DoWork();// AssertmockDependecy.VerifyAll();}}Using the helper GetMockFor, we can setup expectations on the mock dependency. Then, we call the method on the Sut that we are testing and then verify the expectations (in this case, that some dependency actually called the LoadData method.
StructureMap AutoMocking Moq (Updated)
jeremydmiller's original packageStructureMap AutoMocking Moq