gpt4 book ai didi

unit-testing - 甚至可以对 RenderMvcController 进行单元测试?

转载 作者:行者123 更新时间:2023-12-03 14:41:00 24 4
gpt4 key购买 nike

所以我正在使用 Umbraco 6.12 并且很难测试 RenderMvcController .

我已经实现了IApplicationEventHandler在我的Global.ascx并且 Ninject 在运行应用程序时工作正常并且符合预期 - 一切都很好。

但是,对这些 Controller 进行单元测试是另一回事。我找到了这个,并添加了最新的回复:

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

我现在在我的设置中有这个可爱的 hack:

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

原来的 UmbracoContext不能为空,但现在正在抛出:

Current has not been initialized on Umbraco.Web.PublishedCache.PublishedCachesResolver. You must initialize Current before trying to read it.



发布的缓存解析器似乎也隐藏在内部和 protected 东西后面,我不能使用反射来破解,因为我无法初始化任何东西来传递到 SetProperty反射。

这真的很令人沮丧,我喜欢 v6,并且使用 uMapper 非常好。我可以随意向 Controller 注入(inject)一个 repo、服务、命令或查询,而且生活很好——我只是无法覆盖 Controller !

对此的任何帮助将不胜感激。

谢谢。

最佳答案

要对 Umbraco RenderMvcController 进行单元测试,您需要 grab the source code from github ,自己编译解决方案,获取 Umbraco.Tests.dll 并在您的测试项目中引用它。

除此之外,您还需要引用随 Umbraco 包分发的 SQLCE4Umbraco.dll,以及内部用于模拟的 Rhino.Mocks.dll。

为了帮助您解决这个问题,我编译了 Umbraco 6.1.5 的 Umbraco.Tests.dll 并将其与 Rhino.Mocks.dll 放在一起并放在 this zip file .

最后,从 BaseRoutingTest 派生您的测试,覆盖 DatabaseTestBehavior 到
NoDatabasePerFixture,并通过调用GetRoutingContext方法获取UmbracoContext和HttpBaseContext,如下代码:

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;
}
}
}

此代码仅在 Umbraco 6.1.5 中测试过。

关于unit-testing - 甚至可以对 RenderMvcController 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com