设置Unit Test以使用Unity测试控制器

本文关键字:Unity 测试 控制器 Unit Test 设置 | 更新日期: 2023-09-27 18:17:50

我使用Unity创建一个CustomerService的实例。这就是ASP中下面代码的工作。. NET MVC应用程序。

当我创建ASP。. NET MVC应用程序。NET MVC测试项目。我想测试动作(这里:/Home/Index)。问题是我不能设置Unity。我想创建一个真正的动作调用,而不是嘲笑。

你知道如何在测试项目中设置吗?我试过了,但我没有找到正确的语法来创建CustomerService的实例,当我调用控制器时用作参数。

[TestMethod]
public void Index()
{
    var container = UnityConfig.GetConfiguredContainer();
    //I don't find the right syntax for resolve
    ICustomerService customerService = container.Resolve??????
    HomeController controller = new HomeController(customerService);
    ViewResult result = controller.Index() as ViewResult;
    Assert.IsNotNull(result);
    container.Dispose();
}

ASP中使用的工作代码。. NET MVC应用程序

namespace MyTestMVC
{
    public class HomeController : Controller
    {
        private readonly ICustomerService _customerService;
        public HomeController(ICustomerService customerService)
        {
            _customerService = customerService;
        }
        public ActionResult Index()
        {
            var result = _customerService.MyMethod();
            //.....
            return View();
        }
    }
}
//I set Unity like this : 
namespace MyTestMVC.App_Start
{
    public class UnityConfig
    {
        private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
        {
            var container = new UnityContainer();
            RegisterTypes(container);
            return container;
        });
        public static IUnityContainer GetConfiguredContainer()
        {
            return container.Value;
        }
        public static void RegisterTypes(IUnityContainer container)
        {
            container.RegisterType<ICustomerService, CustomerService>();
        }
    }
}
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(MyTestMVC.App_Start.UnityWebActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(MyTestMVC.App_Start.UnityWebActivator), "Shutdown")]
namespace MyTestMVC.App_Start
{
    public static class UnityWebActivator
    {
        /// <summary>Integrates Unity when the application starts.</summary>
        public static void Start() 
        {
            var container = UnityConfig.GetConfiguredContainer();
            FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
            FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        }
        public static void Shutdown()
        {
            var container = UnityConfig.GetConfiguredContainer();
            container.Dispose();
        }
    }
}

设置Unit Test以使用Unity测试控制器

container.Resolve<ICustomerService>();

"如何在完全不启动浏览器的情况下测试一个操作?"

安装Moq到你的单元测试项目

install-package Moq

然后在您的测试中创建iccustomerservice的moq实例并存根所使用的方法

var mock = new Mock<ICustomerService>();
mock.Setup(x => x.MyMethod()).Returns(true);

然后将模拟对象传递给控制器的实例,然后调用控制器上的方法

var controller = new HomeController(mock);
var result = controller.MyMethod();

使用Moq可以断言是否调用了方法等

进一步阅读:https://github.com/Moq/moq4/wiki/Quickstart