Moq服务定位器,用于单元测试

时间:2016-06-04 15:59:21

标签: c# unit-testing moq autofac service-locator

我试图解决这个问题,但没有成功。我怎样才能为这两个测试模拟ProcessorLocator?

这是我的ServiceLocator:

public class ProcessorLocator : IProcessorLocator
{
    public static IContainer Container;

    T IProcessorLocator.GetProcessor<T>()
    {
        return Container.Resolve<T>();
    }

    public static IServiceProvider Init(IServiceCollection services)
    {
        //Autofac
        var containerBuilder = new ContainerBuilder();
        containerBuilder.RegisterModule<ProcessorRegistrationModule>();
        containerBuilder.RegisterType<ProcessorLocator>().As<IProcessorLocator>();
        containerBuilder.Populate(services);
        Container = containerBuilder.Build();

        return Container.Resolve<IServiceProvider>();
    }

    public class ProcessorRegistrationModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            Assembly asm = typeof(ProcessorLocator).GetTypeInfo().Assembly;

            builder.RegisterAssemblyTypes(asm)
                .Where(t => t.Name.EndsWith("Processor"))
                .As(t => t.GetInterfaces().FirstOrDefault(
                    i => i.Name == "I" + t.Name));

            builder.RegisterAssemblyTypes(asm)
                .Where(t => t.Name.EndsWith("Repository"))
                .As(t => t.GetInterfaces().FirstOrDefault(
                    i => i.Name == "I" + t.Name));
        }
    }
}

这是我的课程:

public class MainNavigationViewComponent : ViewComponent
{
    public IProcessorLocator _ProcessorLocator { get; set; }

    public MainNavigationViewComponent(IProcessorLocator processorLocator)
    {
        _ProcessorLocator = processorLocator;
    }       

    public IViewComponentResult Invoke()
    {
        IMenuItemRepository _menuItemRepository = _ProcessorLocator.GetProcessor<IMenuItemRepository>();

        IEnumerable<Entity.MenuItem> _menuItemList = _menuItemRepository.GetAll().Where(x => x.Parent == null && x.onMenu == true);

        return View(_menuItemList.ToList());
    }
}

我的尝试:

[Fact]
public void ActualTest()
{
    //var mockS = new Mock<IProcessorLocator>();
    //ProcessorLocator.Container.SetLocatorProvider(() => mockS.Object);
    var container = BL.ProcessorLocator.InitTest();
    _menuItemMock.Setup(x => x.GetAll()).Returns(new List<MenuItem>() { new MenuItem() { Text = "uu"}, new MenuItem() { Text = "zz" } });

    IMenuItemRepository _menuItemRepository = container.Resolve<IMenuItemRepository>();

     IEnumerable<Entity.MenuItem> _menuItemList = _menuItemRepository.GetAll().Where(x => x.Parent == null && x.onMenu == true);
}

[Fact]
public void MainNavigationViewComponent Test()
{
        //var _subject = new MainNavigationViewComponent(_menuItemMock.Object)
        //{
        //    ActionContext = new ActionContext()
        //    {
        //        HttpContext = new DefaultHttpContext()
        //    }
        //};
}

1 个答案:

答案 0 :(得分:4)

根据您提供的代码:

var fakeMenuRepository = new Mock<IMenuItemRepository>();
fakeMenuRepository.Setup(repository => repository.GetAll())
                  .Returns(new List<MenuItem>()
                  {
                      new MenuItem() { Text = "uu" },
                      new MenuItem() { Text = "zz" }
                  });
var fakeLocator = new Mock<IProcessorLocator>();
fakeLocator.Setup(locator => locator.GetProcessor<IMenuItemRepository>())
           .Returns(fakeMenuRepository.Object);

var target = new MainNavigationViewComponent(fakeLocator.Object);

另一种选择是使用真实容器而不是覆盖IMenuItemRepository的实例;正如我在this answer

中所解释的那样