Is it possible to test the module RenderMvcController?

So, I work with Umbraco 6.12 and with great difficulty I was able to test RenderMvcController .

I implemented IApplicationEventHandler in my Global.ascx , and Ninject works fine and, as expected, when the application starts, everything is fine.

However, unit testing these controllers is another matter. I found this and added the last answer:

http://issues.umbraco.org/issue/U4-1717

Now I have this beautiful hack in my SetUp:

  Umbraco.Web.UmbracoContext.EnsureContext(new HttpContextWrapper(new HttpContext(new HttpRequest("", "http://www.myserver.com", ""), new HttpResponse(null))), ApplicationContext.Current); 

Which will cost the original UmbracoContext may not be null, but now throws:

The current one has not been initialized to Umbraco.Web.PublishedCache.PublishedCachesResolver. You must initialize Current before attempting to read it.

The published cache recognizer also hides behind internal and protected things that I cannot use for reflection, so as not to crack, because I can not start to go into reflection in SetProperty .

This is really frustrating, I love v6, and using uMapper is very nice. I can enter the repo, service, team or request of my choice into the controllers, and life is good - I just can't cover the controllers!

Any help on this would be greatly appreciated.

Thanks.

+6
source share
3 answers

In the unit test Umbraco RenderMvcController you need to take the source code from github , compile the solution yourself and get Umbraco.Tests.dll and the link is in your test project.

In addition to this, you need to refer to SQLCE4Umbraco.dll, which is distributed along with the Umbraco and Rhino.Mocks.dll packages, which is internally for ridicule.

To help you with this, I compiled the Umbraco.Tests.dll file for Umbraco 6.1.5 and put it together with Rhino.Mocks.dll and put it on this zip file .

Finally, get your test out of BaseRoutingTest, override DatabaseTestBehavior for NoDatabasePerFixture and get UmbracoContext and HttpBaseContext by calling the GetRoutingContext method, as in the code below:

 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<IPublishedContent>(); content.Setup(c => c.Id).Returns(id); var model = new RenderModel(content.Object, CultureInfo.InvariantCulture); var blogService = new Mock<IBlogService>(); var entry = new Entry { Id = id }; blogService.Setup(s => s.GetBlogEntry(id)).Returns(entry); var controller = GetBlogEntryController(blogService.Object); var result = (ViewResult)controller.Index(model); blogService.Verify(s => s.GetBlogEntry(id), Times.Once()); Assert.IsNotNull(result); Assert.IsAssignableFrom<Entry>(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; } } } 

This code has been tested only in Umbraco 6.1.5.

+10
source

According to the main command, you must enable the Umbraco.Tests library and inherit your test from BaseUmbracoApplicationTest. This will install a valid UmbracoApplication and UmbracoContext.

https://groups.google.com/forum/?fromgroups=#!topic/umbraco-dev/vEjdzjqmtsU

+1
source

I picked this up on the Umbraco forums, and there are a few answers that may help you.

Look here:

http://our.umbraco.org/forum/developers/api-questions/37255-How-can-I-unit-test-a-class-inheriting-from-SurfaceController

Essentially, you can ... just ... but need some reflection, because some of the key classes and interfaces are internal. As Lukeโ€™s last post notes, this is because functionality is currently a slightly moving target.

0
source

Source: https://habr.com/ru/post/950170/


All Articles