I am working on MVC3 front end. We have an Infrastructure project which is a bridge between UI and Web Service. But Infrastructure is not ready yet. I have views, view models, controllers as below. I have to do unit testing. I have to pass my view model to the infrastructure, so that it will do add, save functionality. From where do I pass the viewmodel to the infrastructure? Is it in controller? If so how? Please provide me a class which will do unit test for the given view, viewmodel, controller. I was asked to mock my view model as the infrastructure and web service are not ready yet so that I can do some testing.
ViewModel
public class BuildRegionModel : IBuildRegionModel
{
#region Constructor / Desctructor
public BuildRegionModel()
{
Name = "User123";
Description = "Long text";
ModifyUser = new User();
ModifyUser.FirstName = "First Name";
StatusSelected = new Status();
ModifyDate = DateTime.Today;
StatusLists = new List<ICode>();
StatusLists.Add(new Status("A","Active"));
StatusLists.Add(new Status("I", "InActive"));
}
~BuildRegionModel()
{
StatusLists = null;
StatusSelected = null;
}
#endregion
#region Properties
public int Id { get; set; }
public string Name { get; set; }
public st开发者_开发知识库ring Description { get; set; }
public List<ICode> StatusLists { get; set; }
public ICode StatusSelected { get; set; }
public IUser ModifyUser { get; set; }
public DateTime ModifyDate { get; set; }
#endregion
}
Controller
public class BuildRegionController : Controller
{
public ActionResult Index(BuildRegionModel model)
{
return View(model);
}
[HttpPost]
public ActionResult Index(BuildRegionModel model, string button)
{
if (button == "Add")
{
}
if (button == "Save")
{
}
if (button == "Cancel")
{
return RedirectToAction("Index", "Home");
}
return View(model);
}
}
I am looking for a test class in this scenario so that I can continue with others.
If your model is really a ViewModel (or in your given context I tend to call them EditModel) you shouldn't pass those on to another layer (not sure what your definition of infrastructure is here).
I sometimes have my controller pass the real business layer object to my EditModel and let the EditModel update the business object. I do this bidirectional, the EditModel will project or pull data from the real model before being send to a view.
I do this to keep my controller methods short and avoid having a lot of left-right mapping code in them.
Any behavior (complexer them simple mutations) or service calls I do keep in my controller (e.g. Save or Add calls against some repository).
What you could unit test is:
Does the ViewModel correctly mutate the (infrastructure? or business) model?
Given the button parameters value is the controller calling the appropriate method on the Infrastructure (you need to define an interface for the infrastructure and mock it).
Does the controller return the correct ActionResult with the correct model data?
Does the controller return the correct ActionResult when the ViewModel doesn't validate or when the (infrastructure? or business) model refuses a change.
Have you considered creating a Save, Add and Cancel method on your controller? It would help both testability and readability if you don't funnel everything through the 'Index' method.
精彩评论