Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since ViewEninges is a static class, you can't mock it with RhinoMocks. I think your best bet is to create a "partial view renderer" interface. An interface is mockable so you'll be able to stub out the complexity of rendering the view. Here's some quick pseudo-code thrown together.</p> <p>First, define the partial view renderer interface:</p> <pre><code>public interface IRenderPartialView { string Render(string viewName, object model); } </code></pre> <p>Then, change your base class' RenderPartialViewToString to be the implementation of IRenderPartialView.Render:</p> <pre><code>public abstract class BaseController : Controller, IRenderPartialView { ... public string Render(string viewName, object model) { // same code as RenderPartialViewToString } } </code></pre> <p>Now we need to change your controller constructors so we can inject an IRenderPartialView during testing -- but use the base class one during production. We can accomplish this by using a pair of constructors:</p> <pre><code>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; } } </code></pre> <p>Now, JsonAdd can call the partial view renderer:</p> <pre><code>public JsonResult JsonAdd(AddPersonViewModel AddPersonModel) { ... return Json(new { Success = true, Message = "The person has been added!", PartialViewHtml = partialRenderer.Render("PersonList", new PersonListViewModel {PersonList = _personList}) }); } </code></pre> <p>So, during testing, you'll mock out an <code>IRenderPartialView</code> and send that to the constructor that accepts an <code>IRenderPartialView</code>. During production, when ASP.NET MVC calls your default constructor, it will use the controller as the renderer (which has the implementation of <code>IRenderPartialView.Render</code> inside the base class).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload