编程语言
首页 > 编程语言> > c#-单元测试的Moq服务定位器

c#-单元测试的Moq服务定位器

作者:互联网

我试图解决这个问题,但没有成功.如何模拟这两个测试的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()
        //    }
        //};
}

解决方法:

根据您提供的代码:

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中解释的

标签:service-locator,unit-testing,moq,c,autofac
来源: https://codeday.me/bug/20191027/1940802.html