ict.ken.be

Delivering solid user friendly software solutions since the dawn of time.

AutoMocking

Categories: Testing

AutoMocking using StructureMap
http://doc.structuremap.net

var autoMocker = new RhinoAutoMocker<StudentRegistrationService>();
var mockStudentValidator = autoMocker.Get<IStudentValidator>();
mockStudentValidator.Stub(x => x.ValidateStudent(Arg<Student>.Is.Anything)).Return(true);

autoMocker.ClassUnderTest.RegisterNewStudent( ... );
autoMocker.Get<IStudentRepository>().AssertWasCalled(x => x.Save(Arg<Student>.Matches( ... )));

AutoMocking using Windsor Castle

public abstract class TestBase
{
static readonly WindsorContainer _mockWindsorContainer;

static TestBase()
{
_mockWindsorContainer = new WindsorContainer();
_mockWindsorContainer.Register(Component.For<LazyComponentAutoMocker>());
}

protected static T MockOf<T>() where T : class
{
return _mockWindsorContainer.Resolve<T>();
}

protected static T Create<T>()
{
_mockWindsorContainer.Register(Component.For<T>());
return _mockWindsorContainer.Resolve<T>();
}
}

public class LazyComponentAutoMocker : ILazyComponentLoader
{
public IRegistration Load(string key, Type service, IDictionary arguments)
{
return Component.For(service).Instance(MockRepository.GenerateStub(service));
}
}

AutoMocking with Ninject
http://trycatchfail.com/blog/post/The-Right-Way-to-Do-Automocking-with-Ninject.aspx

AutoMocking with Moq
http://moqcontrib.codeplex.com/