如何在StructureMap AutoMocker中使用录制/播放语法?

时间:2022-06-05 20:02:45

Is it possible to use the syntax

是否可以使用语法

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

with StructureMap RhinoAutoMocker?

使用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.

由于RhinoAutoMocker继承了MockRepository,因此在结果映射2.5中的Jeremy Millers原始帖子AutoMocker中,这似乎是可能的,但在StructureMap的2.5.2版本中,这似乎是以稍微不同的方式实现的。

1 个解决方案

#1


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

我终于使用自定义编写的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();
    }
}

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.

我仍然不知道是否有内置的方法来做到这一点。但是这样可以避免重写1200次测试。

#1


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

我终于使用自定义编写的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();
    }
}

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.

我仍然不知道是否有内置的方法来做到这一点。但是这样可以避免重写1200次测试。