如何测试辅助方法?

发布于 2024-11-05 11:30:43 字数 529 浏览 0 评论 0原文

我已经制作了一个助手,

public static class UrlHelperExtension
{
    public static string GetContent(this UrlHelper url, string link, bool breakCache = true)
    {
        // stuff
    }
}

如何在单元测试中测试它?

[TestClass]
public class HelperTestSet
{
    [TestMethod]
    public void GetContentUrl()
    {
        // What do I need to do here?
        // I need a RequestContext to create a new UrlHelper
        // Which is the simplest way to test it?
    }
}

如何创建测试所需的助手?

I've made a helper

public static class UrlHelperExtension
{
    public static string GetContent(this UrlHelper url, string link, bool breakCache = true)
    {
        // stuff
    }
}

How do I test it in a unit test?

[TestClass]
public class HelperTestSet
{
    [TestMethod]
    public void GetContentUrl()
    {
        // What do I need to do here?
        // I need a RequestContext to create a new UrlHelper
        // Which is the simplest way to test it?
    }
}

How do I create the helper needed for the test?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无敌元气妹 2024-11-12 11:30:43

如果你真的想完全解耦地测试它,你必须引入另一层抽象。所以在你的情况下你可以这样做:

public interface IUrlHelper 
{
    public string Action(string actionName);

    // Add other methods you need to use in your extension method.
}

public class UrlHelperAdapter : IUrlHelper
{
    private readonly UrlHelper urlHelper;

    public UrlHelperAdapter(UrlHelper urlHelper)
    {
        this.urlHelper = urlHelper;
    }

    string IUrlHelper.Action(string actionName)
    {
        return this.urlHelper.Action(actionName);
    }
}

public static class UrlHelperExtension
{
    public static string GetContent(this UrlHelper url, string link, bool breakCache = true)
    {
        return GetContent(new UrlHelperAdapter(url), link, breakCache); 
    }

    public static string GetContent(this IUrlHelper url, string link, bool breakCache =     true)
    {
        // Do the real work on IUrlHelper
    }
}

[TestClass]
public class HelperTestSet
{
    [TestMethod]
    public void GetContentUrl()
    {
        string expected = "...";

        IUrlHelper urlHelper = new UrlHelperMock();

        string  actual = urlHelper.GetContent("...", true);

        Assert.AreEqual(expected, actual);
    }
}

If you really want to test it totally uncoupled, you have to introduce another layer of abstraction. So in your case you could do something like this:

public interface IUrlHelper 
{
    public string Action(string actionName);

    // Add other methods you need to use in your extension method.
}

public class UrlHelperAdapter : IUrlHelper
{
    private readonly UrlHelper urlHelper;

    public UrlHelperAdapter(UrlHelper urlHelper)
    {
        this.urlHelper = urlHelper;
    }

    string IUrlHelper.Action(string actionName)
    {
        return this.urlHelper.Action(actionName);
    }
}

public static class UrlHelperExtension
{
    public static string GetContent(this UrlHelper url, string link, bool breakCache = true)
    {
        return GetContent(new UrlHelperAdapter(url), link, breakCache); 
    }

    public static string GetContent(this IUrlHelper url, string link, bool breakCache =     true)
    {
        // Do the real work on IUrlHelper
    }
}

[TestClass]
public class HelperTestSet
{
    [TestMethod]
    public void GetContentUrl()
    {
        string expected = "...";

        IUrlHelper urlHelper = new UrlHelperMock();

        string  actual = urlHelper.GetContent("...", true);

        Assert.AreEqual(expected, actual);
    }
}
嘿咻 2024-11-12 11:30:43

阅读以下网址可能会对您有所帮助。
测试扩展方法

Read the following URl may help you.
test the extension methods

憧憬巴黎街头的黎明 2024-11-12 11:30:43

这个网站我想出了

namespace Website.Tests
{
    public static class UrlHelperExtender
    {
        public static string Get(this UrlHelper url)
        {
            return "a";
        }
    }

    [TestClass]
    public class All
    {
        private class FakeHttpContext : HttpContextBase
        {
            private Dictionary<object, object> _items = new Dictionary<object, object>();
            public override IDictionary Items { get { return _items; } }
        }

        private class FakeViewDataContainer : IViewDataContainer
        {
            private ViewDataDictionary _viewData = new ViewDataDictionary();
            public ViewDataDictionary ViewData { get { return _viewData; } set { _viewData = value; } }
        }

        [TestMethod]
        public void Extension()
        {
            var vc = new ViewContext();
            vc.HttpContext = new FakeHttpContext();
            vc.HttpContext.Items.Add("wtf", "foo");

            var html = new HtmlHelper(vc, new FakeViewDataContainer());
            var url = new UrlHelper(vc.RequestContext);

            var xx = url.Get();

            Assert.AreEqual("a", xx);
        }
    }
}

From this website I came up with

namespace Website.Tests
{
    public static class UrlHelperExtender
    {
        public static string Get(this UrlHelper url)
        {
            return "a";
        }
    }

    [TestClass]
    public class All
    {
        private class FakeHttpContext : HttpContextBase
        {
            private Dictionary<object, object> _items = new Dictionary<object, object>();
            public override IDictionary Items { get { return _items; } }
        }

        private class FakeViewDataContainer : IViewDataContainer
        {
            private ViewDataDictionary _viewData = new ViewDataDictionary();
            public ViewDataDictionary ViewData { get { return _viewData; } set { _viewData = value; } }
        }

        [TestMethod]
        public void Extension()
        {
            var vc = new ViewContext();
            vc.HttpContext = new FakeHttpContext();
            vc.HttpContext.Items.Add("wtf", "foo");

            var html = new HtmlHelper(vc, new FakeViewDataContainer());
            var url = new UrlHelper(vc.RequestContext);

            var xx = url.Get();

            Assert.AreEqual("a", xx);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文