我正在努力对以下代码进行单元测试。它是用于初始化应用程序中的mvc控制器的控制器工厂。谁能给我一些关于如何进行单元测试的指示?
public class WindsorControllerFactory : DefaultControllerFactory
{
private readonly IKernel kernel;
public WindsorControllerFactory(IKernel kernel)
{
this.kernel = kernel;
}
public override void ReleaseController(IController controller)
{
this.kernel.ReleaseComponent(controller);
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path '{0}' could not be found.", requestContext.HttpContext.Request.Path));
}
return (IController)this.kernel.Resolve(controllerType);
}
}
答案 0 :(得分:0)
除非您决定覆盖
IController CreateController(RequestContext requestContext,string controllerName)
您的测试变得更加复杂,因为原始CreateController调用GetControllerType
这么说,我必须同意Ales ...但是如果你真的必须...你可以使用像Moq这样的模拟框架轻松地单元测试内核调用
假设您想要测试ReleaseController方法
var controller = new Mock<IController>();
var kernel= new Mock<IKernel>();
var windsorControllerFactory = new WindsorControllerFactory(kernel.Object);
windsorControllerFactory.ReleaseController(controller.Object);
kernel.Verify(m => m.ReleaseComponent(controller.Object));