In a MVC5 website I have created some unit tests for the controller methods and mocked the data using Moq for Business methods which are called from Controller. In every article which said about setting up unit tests for Controllers it has a step to download Unity.MVC5 component and register the types for DI.
But when I comment the call for RegisterComponents(), the unit tests still execute perfectly with mocked data and the application also works. So do I need this Unity.MVC5 component?
Note that in Controller I have kept both parametered and parameterless contructors.
Code: Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//UnityConfig.RegisterComponents(); //commented the use of Unity.MVC5
}
Unit test:
[Test]
public void GetDiversionLog_Returns_Some_Data_Selected_Department()
{
DashboardController dashboardController = Mock_InitialiseControllerWithSession();
DataSourceRequest request = new DataSourceRequest();
ActionResult result = dashboardController.GetDiversionLog(request, "03-08-2015 22:45", 100610);
Assert.IsNotNull(result);
Assert.AreEqual(3, result.Total);
}
Controller:
public class DashboardController : BaseController
{
private readonly IDiversionLogBusiness _diversionLogBusiness;
private readonly IDashboardBusiness _dashboardBusiness;
public DashboardController()
//: this(new DiversionLogBusiness())
{
_diversionLogBusiness = new DiversionLogBusiness();
_dashboardBusiness = new DashboardBusiness();
}
public DashboardController(IDiversionLogBusiness diversionLogBusiness, IDashboardBusiness dashboardBusiness)
{
_diversionLogBusiness = diversionLogBusiness;
_dashboardBusiness = dashboardBusiness;
}
.
.
.
}
Aucun commentaire:
Enregistrer un commentaire