测试的WebAPI控制器Url.Link [英] Testing WebApi Controller Url.Link

查看:158
本文介绍了测试的WebAPI控制器Url.Link的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器动作

public void Post(Dto model)
{
    using (var message = new MailMessage())
    {
        var link = Url.Link("ConfirmAccount", new { model.Id });

        message.To.Add(model.ToAddress);
        message.IsBodyHtml = true;
        message.Body = string.Format(@"<p>Click <a href=""{0}"">here</a> to complete your registration.<p><p>You may also copy and paste this link into your browser.</p><p>{0}</p>", link);

        MailClient.Send(message);
    }
}

要测试这一点,我需要设置控制器上下文

To test this I need to setup the controller context

var httpConfiguration = new HttpConfiguration(new HttpRouteCollection { { "ConfirmAccount", new HttpRoute() } });
var httpRouteData = new HttpRouteData(httpConfiguration.Routes.First());
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "http://localhost");
sut = new TheController
{
    ControllerContext = new HttpControllerContext(httpConfiguration, httpRouteData, httpRequestMessage),
    MailClient = new SmtpClient { PickupDirectoryLocation = location }
};

这似乎是一个很大的设置来测试链路的创造。是否有一个更清洁的方式做到这一点?我看了一下内存的服务器,但看起来像它更适用于HttpClient的不是直接测试控制器。

This seems like a lot of setup to test the creation of a link. Is there a cleaner way to do this? I have read about in-memory servers but that looks like it applies more to the httpclient than testing the controller directly.

推荐答案

下面是测试 UrlHelper 没有任何一种嘲弄库所需的绝对最低code。这把我(和我花了一些时间来追查)的事情是,你需要设置 IHttpRouteData 的要求。如果你没有在 IHttpRoute 实例将无法生成造成空URL虚拟路径。

Below is the absolute minimum code required to test UrlHelper without any kind of mocking library. The thing that threw me (and took me some time to track down) was that you need to set the IHttpRouteData of the request. If you don't the IHttpRoute instance will fail to generate a virtual path resulting in an empty URL.

    public class FooController : ApiController
    {
        public string Get()
        {
            return Url.Link(RouteNames.DefaultRoute, new { controller = "foo", id = "10" });
        }
    }

    [TestFixture]
    public class FooControllerTests
    {
        FooController controller;

        [SetUp]
        public void SetUp()
        {
            var config = new HttpConfiguration();

            config.Routes.MapHttpRoute(
                name: "Default",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional });

            var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost");
            request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
            request.Properties[HttpPropertyKeys.HttpRouteDataKey] = new HttpRouteData(new HttpRoute());

            controller = new FooController
            {
                Request = request
            };
        }

        [Test]
        public void Get_returns_link()
        {
            Assert.That(controller.Get(), Is.EqualTo("http://localhost/api/foo/10"));
        }
    }

这篇关于测试的WebAPI控制器Url.Link的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆