Since ViewEninges is a static class, you cannot mock it with RhinoMocks. I find it best to create a "partial image viewer" interface. The interface is a mock one, so you can limit the complexity of rendering a view. Here are some quick pseudo codes thrown together.
First, define the partial view visualization interface:
public interface IRenderPartialView { string Render(string viewName, object model); }
Then change the base class RenderPartialViewToString to the implementation of IRenderPartialView.Render:
public abstract class BaseController : Controller, IRenderPartialView { ... public string Render(string viewName, object model) {
Now we need to change the designers of the controller so that we can implement IRenderPartialView during testing, but use the base class in the production process. We can do this using a couple of constructors:
public class YourController : BaseController { private IRenderPartialView partialRenderer; public YourController() { SetRenderer(this); } public YourController(IRenderPartialView partialRenderer) { SetRenderer(partialRenderer); } private void SetRenderer(IRenderPartialView partialRenderer) { this.partialRenderer = this; } }
Now JsonAdd can invoke a partial view renderer:
public JsonResult JsonAdd(AddPersonViewModel AddPersonModel) { ... return Json(new { Success = true, Message = "The person has been added!", PartialViewHtml = partialRenderer.Render("PersonList", new PersonListViewModel {PersonList = _personList}) }); }
So, during testing, you will mock IRenderPartialView and send this to a constructor that accepts IRenderPartialView . During production, when ASP.NET MVC calls your default constructor, it will use the controller as a visualization tool (having an implementation of IRenderPartialView.Render inside the base class).
PatrickSteele
source share