Как тестировать TagHelpers, используя только ввод и вывод? - PullRequest
1 голос
/ 30 сентября 2019

Следующие тесты работают, но кажется глупым передавать все эти значения объекта в качестве параметров вместо того, чтобы писать тесты только с входными и выходными параметрами. В https://github.com/aspnet/AspNetCore/blob/master/src/Mvc/Mvc.TagHelpers/test/ я нашел только похожие тестовые случаи - должен быть какой-то способ сделать это?

Тест:

using System;
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using MyNamespace.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Shouldly;
using Xunit;

namespace MyNamespace.Tests
{
    public class ViewableTagHelperTests
    {
        [Theory]
        [InlineData("<a href='#'>test</a>", "a", "a", true, null, "<a href='#'>test</a>")]
        [InlineData("<a href='#'>test</a>", "a", null, null, true, "")]
        [InlineData("<a href='#'>test</a>", "a", null, true, true, "")]
        [InlineData("<a href='#'>test</a>", "a", null, null, null, "")]
        [InlineData("<input href='#'/>", "input", "input", true, null, "<input href='#'/>")]
        [InlineData("<input href='#'/>", "input", null, null, true, "")]
        [InlineData("<input href='#'/>", "input", null, true, true, "")]
        [InlineData("<input href='#'/>", "input", null, null, null, "")]
        public void VerifyProperties(string input, string tag, string expectedTag, bool? show, bool? hide, string expectedOutput)
        {
            var viewableTagHelper = new ViewableTagHelper();
            var context = new TagHelperContext(tag, new TagHelperAttributeList
            {
                new TagHelperAttribute("display-if", show.HasValue ? show.Value ? "true" : "false" : "null"),
                new TagHelperAttribute("hide-if", hide.HasValue ? hide.Value ? "true" : "false" : "null"),
            }, new Dictionary<object, object>(), "1");
            var ouput = new TagHelperOutput(tag, new TagHelperAttributeList(), (b, encoder) =>
            {
                return Task.FromResult<TagHelperContent>(null);
            });

            ouput.Content.AppendHtml(input);
            viewableTagHelper.DisplayIf = show;
            viewableTagHelper.HideIf = hide;
            viewableTagHelper.Process(context, ouput);

            var content = ouput.Content.GetContent();

            content.ShouldBe(expectedOutput);
            ouput.TagName.ShouldBe(expectedTag);
        }
    }
}

TagHelper:

[HtmlTargetElement(Attributes = "hide-if")]
[HtmlTargetElement(Attributes = "display-if")]
public class ViewableTagHelper : TagHelper
{
    [HtmlAttributeName("hide-if")]
    public bool? HideIf { get; set; }

    [HtmlAttributeName("display-if")]
    public bool? DisplayIf { get; set; }

    /// <inheritdoc />
    public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        bool diplay = true;
        if (diplay && HideIf.HasValue)
            diplay = !HideIf.Value;
        if (diplay && DisplayIf.HasValue)
            diplay = DisplayIf.Value;
        if (!DisplayIf.HasValue && !HideIf.HasValue)
            diplay = false;

        if (!diplay)
            output.SuppressOutput();
    }
}
...