简体   繁体   English

AutoMockContainer,支持具有非接口依赖性的automocking类

[英]AutoMockContainer with support for automocking classes with non-interface dependencies

I have a constructor which has a non-interface dependency: 我有一个具有非接口依赖的构造函数:

public MainWindowViewModel(IWorkItemProvider workItemProvider, WeekNavigatorViewModel weekNavigator)

I am using the Moq.Contrib automockcontainer. 我正在使用Moq.Contrib automockcontainer。 If I try to automock the MainWindowViewModel class, I get an error due to the WeekNavigatorViewModel dependency. 如果我尝试自动锁定MainWindowViewModel类,由于WeekNavigatorViewModel依赖项,我收到错误。

Are there any automocking containers which supports mocking of non-interface types? 是否有任何automocking容器支持非接口类型的模拟?

As Mark has shown below; 正如马克在下面所示; yes you can! 是的你可以! :-) I replaced the Moq.Contrib AutoMockContainer with the stuff Mark presents in his answer, the only difference is that the auto-generated mocks are registered as singletons, but you can make this configurable. :-)我将Moq.Contrib AutoMockContainer替换为Mark在他的答案中提出的东西,唯一的区别是自动生成的模拟被注册为单例,但你可以使这个可配置。 Here is the final solution: 这是最终的解决方案:

/// <summary>
/// Auto-mocking factory that can create an instance of the 
/// class under test and automatically inject mocks for all its dependencies.
/// </summary>
/// <remarks>
/// Mocks interface and class dependencies
/// </remarks>
public class AutoMockContainer
{
    readonly IContainer _container;

    public AutoMockContainer(MockFactory factory)
    {
        var builder = new ContainerBuilder();

        builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
        builder.RegisterSource(new MoqRegistrationSource(factory));

        _container = builder.Build();
    }

    /// <summary>
    /// Gets or creates a mock for the given type, with 
    /// the default behavior specified by the factory.
    /// </summary>
    public Mock<T> GetMock<T>() where T : class
    {
        return (_container.Resolve<T>() as IMocked<T>).Mock;
    }

    /// <summary>
    /// Creates an instance of a class under test, 
    /// injecting all necessary dependencies as mocks.
    /// </summary>
    /// <typeparam name="T">Requested object type.</typeparam>
    public T Create<T>() where T : class
    {
        return _container.Resolve<T>();
    }

    public T Resolve<T>()
    {
        return _container.Resolve<T>();
    }

    /// <summary>
    /// Registers and resolves the given service on the container.
    /// </summary>
    /// <typeparam name="TService">Service</typeparam>
    /// <typeparam name="TImplementation">The implementation of the service.</typeparam>
    public void Register<TService, TImplementation>()
    {
        var builder = new ContainerBuilder();

        builder.RegisterType<TImplementation>().As<TService>().SingleInstance();
        builder.Update(_container);
    }

    /// <summary>
    /// Registers the given service instance on the container.
    /// </summary>
    /// <typeparam name="TService">Service type.</typeparam>
    /// <param name="instance">Service instance.</param>
    public void Register<TService>(TService instance)
    {
        var builder = new ContainerBuilder();

        if (instance.GetType().IsClass)
            builder.RegisterInstance(instance as object).As<TService>();
        else
            builder.Register(c => instance).As<TService>();

        builder.Update(_container);
    }

    class MoqRegistrationSource : IRegistrationSource
    {
        private readonly MockFactory _factory;
        private readonly MethodInfo _createMethod;

        public MoqRegistrationSource(MockFactory factory)
        {
            _factory = factory;
            _createMethod = factory.GetType().GetMethod("Create", new Type[] { });
        }

        public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
        {
            var swt = service as IServiceWithType;
            if (swt == null)
            {
                yield break;
            }

            if (!swt.ServiceType.IsInterface)
                yield break;

            var existingReg = registrationAccessor(service);
            if (existingReg.Any())
            {
                yield break;
            }

            var reg = RegistrationBuilder.ForDelegate((c, p) =>
            {
                var createMethod = _createMethod.MakeGenericMethod(swt.ServiceType);
                return ((Mock)createMethod.Invoke(_factory, null)).Object;
            }).As(swt.ServiceType).SingleInstance().CreateRegistration();

            yield return reg;
        }

        public bool IsAdapterForIndividualComponents
        {
            get { return false; }
        }
    }
}

You can pretty easily write one yourself if you leverage a DI Container that supports just-in-time resolution of requested types. 如果您利用支持即时解析所请求类型的DI容器 ,您可以很容易地自己编写一个。

I recently wrote a prototype for exactly that purpose using Autofac and Moq, but other containers could be used instead. 我最近使用Autofac和Moq为这个目的编写了一个原型,但是可以使用其他容器。

Here's the appropriate IRegistrationSource: 这是适当的IRegistrationSource:

public class AutoMockingRegistrationSource : IRegistrationSource
{
    private readonly MockFactory mockFactory;

    public AutoMockingRegistrationSource()
    {
        this.mockFactory = new MockFactory(MockBehavior.Default);
        this.mockFactory.CallBase = true;
        this.mockFactory.DefaultValue = DefaultValue.Mock;
    }

    public MockFactory MockFactory
    {
        get { return this.mockFactory; }
    }

    #region IRegistrationSource Members

    public IEnumerable<IComponentRegistration> RegistrationsFor(
        Service service,
        Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
    {
        var swt = service as IServiceWithType;
        if (swt == null)
        {
            yield break;
        }

        var existingReg = registrationAccessor(service);
        if (existingReg.Any())
        {
            yield break;
        }

        var reg = RegistrationBuilder.ForDelegate((c, p) =>
            {
                var createMethod = 
                    typeof(MockFactory).GetMethod("Create", Type.EmptyTypes).MakeGenericMethod(swt.ServiceType);
                return ((Mock)createMethod.Invoke(this.MockFactory, null)).Object;
            }).As(swt.ServiceType).CreateRegistration();

        yield return reg;
    }

    #endregion
}

You can now set up the container in a unit test like this: 您现在可以在单元测试中设置容器,如下所示:

[TestMethod]
public void ContainerCanCreate()
{
    // Fixture setup
    var builder = new ContainerBuilder();
    builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
    builder.RegisterSource(new AutoMockingRegistrationSource());
    var container = builder.Build();
    // Exercise system
    var result = container.Resolve<MyClass>();
    // Verify outcome
    Assert.IsNotNull(result);
    // Teardown
}

That's all you need to get started. 这就是你开始所需要的一切。

MyClass is a concrete class with an abstract dependency. MyClass是一个具有抽象依赖的具体类。 Here is the constructor signature: 这是构造函数签名:

public MyClass(ISomeInterface some)

Notice that you don't have to use Autofac (or any other DI Container) in your production code. 请注意,您不必在生产代码中使用Autofac(或任何其他DI容器)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM