简体   繁体   English

我可以动态地将数据注释添加到视图模型以进行单元测试吗?

[英]Can I dynamically add Data Annotation to a view model for unit-testing?

I am to write a series tests for a custom HtmlHelper . 我要为自定义HtmlHelper编写一系列测试。

A mocked view model is created (with no DataAnnotation ): 创建一个模拟视图模型(不带DataAnnotation ):

    private sealed class MockLoginInputModel
    {
        public string Username { get; set; }
    }

Then there are a simplest test: 然后有一个最简单的测试:

    [Test]
    public void SimplestLabel()
    {

        //arrange
        var sb = new StringBuilder();
        sb.Append(@"<label for=""Username"">");
        sb.Append(@"Username");
        sb.Append(@"</label>");
        var expected = sb.ToString();

        var html = HtmlHelperFactory.Create(new MockLoginInputModel());

        //act
        var result = html.WviLabelFor(m => m.Username);

        //assert
        var actualOutput = result.ToHtmlString();
        Assert.AreEqual(actualOutput, expected);
    }

After the simplest, I am to write a test to see whether the DataAnnotation features are functional. 最简单的情况是,我要编写一个测试以查看DataAnnotation功能是否正常运行。

Of course we can create another mock model with some data annotation, like: 当然,我们可以创建另一个带有某些数据注释的模拟模型,例如:

    private sealed class MockLoginInputModel
    {
        [DisplayName("Your Username: ")]
        public string Username { get; set; }
    }

But we would have to create quite a few models for different tests. 但是我们必须为不同的测试创建很多模型。

Is there a way we can just add in the data annotation attribute 有没有一种方法可以只添加数据注释属性

( [DisplayName("Your Username: ")] ) in the testing methods? [DisplayName("Your Username: ")] )在测试方法中?

Something like: 就像是:

    var model=new MockLoginInputModel();     
    model=AddDisplayName(model, "Your username:");             
    var html = HtmlHelperFactory.Create(model);

You cannot do this as far as I can tell. 据我所知,您不能这样做。 Would be great if someone could prove me wrong. 如果有人可以证明我错了,那就太好了。

See this question and this question . 看到这个问题这个问题

Since you are running a unit-test, you could do something like: 由于您正在运行单元测试,因此可以执行以下操作:

public static readonly unformattedClass = "public class {0} {{ {1}public string MyString {{ get; set; }} }

Unit test: 单元测试:

[Test]
public void SimplestLabel()
{
   var className = "SimplestLabelClass";
   var dataAnnotation = "[DisplayName(\"Your Username: \")]";

   var formattedClass = string.Format(unformattedClass,
     className,
     dataAnnotation);

   var model = CreateDynamicModel(formattedClass, className);

   var html = HtmlHelperFactory.Create(model);
}

private object CreateDynamicModel(string formattedClass, string className)
{    
  object result;
  using (var csharp = new Microsoft.CSharp.CSharpCodeProvider())
  {
    var res = csharp.CompileAssemblyFromSource(
        new System.CodeDom.Compiler.CompilerParameters() 
        {  
            GenerateInMemory = true 
        }, 
        formattedClass
    );

    var classType = res.CompiledAssembly.GetType(className);

    result = Activator.CreateInstance(type);
  }

  return result;
}       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM