c# – AutoMockContainer,支持具有非接口依赖关系的自动化类
发布时间:2020-12-15 03:59:58 所属栏目:百科 来源:网络整理
导读:我有一个具有非接口依赖性的构造函数: public MainWindowViewModel(IWorkItemProvider workItemProvider,WeekNavigatorViewModel weekNavigator) 我正在使用Moq.Contrib automockcontainer.如果我尝试自动化MainWindowViewModel类,由于WeekNavigatorViewMod
我有一个具有非接口依赖性的构造函数:
public MainWindowViewModel(IWorkItemProvider workItemProvider,WeekNavigatorViewModel weekNavigator) 我正在使用Moq.Contrib automockcontainer.如果我尝试自动化MainWindowViewModel类,由于WeekNavigatorViewModel依赖关系,我收到一个错误. 是否有任何支持嘲笑非接口类型的自动容器? 正如马克以下所示;是的你可以! :-)我替换了Moq.Contrib AutoMockContainer与他的答案的标记礼物,唯一的区别是,自动生成的模拟注册为单身,但你可以使这个可配置.这是最终的解决方案: /// <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; } } } } 解决方法
如果您利用支持即时解决请求类型的DI容器,您可以很容易地自己编写一个.
我最近为Autofac和Moq写了一个原型,但是可以使用其他容器. 这是适当的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,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 } 您现在可以在单元测试中设置容器,如下所示: [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 } 这就是你需要开始的. MyClass是一个具有抽象依赖关系的具体类.这里是构造函数签名: public MyClass(ISomeInterface some) 请注意,您不必在生产代码中使用Autofac(或任何其他DI Container). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |