You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
An automocking container for Moq. Use this if you're invested in your IoC
container and want to decouple your unit tests from changes to their
constructor arguments.
Usage
Simplest usage is to build an instance that you can unit test.
If you have a special instance that you need to use, you can register it
with .Use(...). This is very similar to registrations in a regular IoC
container (i.e. For<IService>().Use(x) in StructureMap).
varmocker=newAutoMocker();mocker.Use<IDriveTrain>(newDriveTrain());// OR, setup a Mockmocker.Use<IDriveTrain>(x =>x.Shaft.Length==5);varcar=mocker.CreateInstance<Car>();
Extracting Mocks
At some point you might need to get to a mock that was auto-generated. For
this, use the .Get<>() or .GetMock<>() methods.
varmocker=newAutoMocker();// Let's say you have a setup that needs verifyingmocker.Use<IDriveTrain>(x =>x.Accelerate(42)==true);varcar=mocker.CreateInstance<Car>();car.Accelerate(42);// Then extract & verifyvardriveTrainMock=mocker.GetMock<IDriveTrain>();driveTrainMock.VerifyAll();
Alternately, there's an even faster way to verify all mocks in the container:
varmocker=newAutoMocker();mocker.Use<IDriveTrain>(x =>x.Accelerate(42)==true);varcar=mocker.CreateInstance<Car>();car.Accelerate(42);// This method verifies all mocks in the containermocker.VerifyAll();