我最终使用自定义编写的AutoMocker和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();
}
}
我仍然不知道是否有内置的方法来完成这个。但这个方法可行,并且让我避免了重写1200个测试。