I am to write a series tests for a custom HtmlHelper
.
A mocked view model is created (with no 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.
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?
Something like:
var model=new MockLoginInputModel();
model=AddDisplayName(model, "Your username:");
var html = HtmlHelperFactory.Create(model);