views:

165

answers:

1

Is it possible to use the syntax

using(_mocks.Record())
{
   //...
}
using(_mocks.Playback())
{
   //...
}

with StructureMap RhinoAutoMocker?

In Jeremy Millers original post AutoMocker in StructureMap 2.5 this seems possible since RhinoAutoMocker inherits MockRepository, but in version 2.5.2 of StructureMap this seems to be implemented in a slightly different way.

+1  A: 

I finally solved this using a custom written AutoMocker and ServiceLocator.

public class RecordPlaybackRhinoAutoMocker<TARGETCLASS> : AutoMocker<TARGETCLASS> where TARGETCLASS : class
{
    private RecordPlaybackMocksServiceLocator MockRepository 
    { 
        get 
        { 
            return _serviceLocator as RecordPlaybackMocksServiceLocator; 
        } 
    }

    public RecordPlaybackRhinoAutoMocker()
    {
        _serviceLocator = new RecordPlaybackMocksServiceLocator();
        _container = new AutoMockedContainer(_serviceLocator);
    }

    public IDisposable Record()
    {
        return MockRepository.Record();
    }

    public IDisposable Playback()
    {
        return MockRepository.Playback();
    }
}

public class RecordPlaybackMocksServiceLocator : StructureMap.AutoMocking.ServiceLocator
{

    private readonly MockRepository _mocks;

    public RecordPlaybackMocksServiceLocator()
    {
        _mocks = new MockRepository();
    }

    public T PartialMock<T>(params object[] args) where T : class
    {
        return _mocks.PartialMock<T>(args);
    }

    public object Service(Type serviceType)
    {
        return _mocks.StrictMock(serviceType);
    }

    public T Service<T>() where T : class
    {
        return _mocks.StrictMock<T>();
    }

    public IDisposable Record()
    {
        return _mocks.Record();
    }

    public IDisposable Playback()
    {
        return _mocks.Playback();
    }
}

I still don't know if there is a built in way to do this. But this works and saves me from rewritting 1200 tests.

maz