Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To unit test a Umbraco RenderMvcController, you need to <a href="https://github.com/umbraco/Umbraco-CMS">grab the source code from github</a>, compile the solution yourself, and get the Umbraco.Tests.dll and reference it on your test project.</p> <p>In addition to that, you need to reference the SQLCE4Umbraco.dll which is distributed with the Umbraco packages, and Rhino.Mocks.dll which is internally for mocking.</p> <p>To help you with this, I have compiled put the Umbraco.Tests.dll for Umbraco 6.1.5 and put it together with the Rhino.Mocks.dll and put it on <a href="http://jlusar.es/get/umbraco%20mvc%20test/UmbracoTest.zip">this zip file</a>.</p> <p>Finally, derive your test from BaseRoutingTest, override the DatabaseTestBehavior to NoDatabasePerFixture, and get the UmbracoContext and HttpBaseContext by calling the GetRoutingContext method, as in the code below:</p> <pre><code>using System; using Moq; using NUnit.Framework; using System.Globalization; using System.Web.Mvc; using System.Web.Routing; using Umbraco.Core.Models; using Umbraco.Tests.TestHelpers; using Umbraco.Web; using Umbraco.Web.Models; using Umbraco.Web.Mvc; namespace UnitTests.Controllers { public class Entry { public int Id { get; set; } public string Url { get; set; } public string Title { get; set; } public string Summary { get; set; } public string Content { get; set; } public string Author { get; set; } public string[] Tags { get; set; } public DateTime Date { get; set; } } public interface IBlogService { Entry GetBlogEntry(int id); } public class BlogEntryController : RenderMvcController { private readonly IBlogService _blogService; public BlogEntryController(IBlogService blogService, UmbracoContext ctx) : base(ctx) { _blogService = blogService; } public BlogEntryController(IBlogService blogService) : this(blogService, UmbracoContext.Current) { } public override ActionResult Index(RenderModel model) { var entry = _blogService.GetBlogEntry(model.Content.Id); // Test will fail if we return CurrentTemplate(model) as is expecting // the action from ControllerContext.RouteData.Values["action"] return View("BlogEntry", entry); } } [TestFixture] public class RenderMvcControllerTests : BaseRoutingTest { protected override DatabaseBehavior DatabaseTestBehavior { get { return DatabaseBehavior.NoDatabasePerFixture; } } [Test] public void CanGetIndex() { const int id = 1234; var content = new Mock&lt;IPublishedContent&gt;(); content.Setup(c =&gt; c.Id).Returns(id); var model = new RenderModel(content.Object, CultureInfo.InvariantCulture); var blogService = new Mock&lt;IBlogService&gt;(); var entry = new Entry { Id = id }; blogService.Setup(s =&gt; s.GetBlogEntry(id)).Returns(entry); var controller = GetBlogEntryController(blogService.Object); var result = (ViewResult)controller.Index(model); blogService.Verify(s =&gt; s.GetBlogEntry(id), Times.Once()); Assert.IsNotNull(result); Assert.IsAssignableFrom&lt;Entry&gt;(result.Model); } private BlogEntryController GetBlogEntryController(IBlogService blogService) { var routingContext = GetRoutingContext("/test"); var umbracoContext = routingContext.UmbracoContext; var contextBase = umbracoContext.HttpContext; var controller = new BlogEntryController(blogService, umbracoContext); controller.ControllerContext = new ControllerContext(contextBase, new RouteData(), controller); controller.Url = new UrlHelper(new RequestContext(contextBase, new RouteData()), new RouteCollection()); return controller; } } } </code></pre> <p>This code has only been tested in Umbraco 6.1.5.</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.
 

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