使用 HtmlHelper 进行 DisplayFormat 单元测试

发布于 2024-11-25 08:49:29 字数 606 浏览 0 评论 0原文

MyModel _model = new MyModel() { PriceDate = new DateTime(2000, 1, 1)};

var helper = new System.Web.Mvc.HtmlHelper<MyModel>(new ViewContext(), new ViewPage());
var result = helper.DisplayFor(m => _model.PriceDate);

Assert.That(result, Is.EqualTo(expected));

我想测试调用 DisplayFor 生成的输出是否采用以下指定的格式...

[DisplayFormat(DataFormatString = "{0:dd/MM/yy}")]
public DateTime? PriceDate { get; set; }

代码编译但失败,并在 DisplayFor 处出现 NullReferenceException >。

谁能帮我完成这项工作吗?

(注:这是一个更大问题的小例子)

MyModel _model = new MyModel() { PriceDate = new DateTime(2000, 1, 1)};

var helper = new System.Web.Mvc.HtmlHelper<MyModel>(new ViewContext(), new ViewPage());
var result = helper.DisplayFor(m => _model.PriceDate);

Assert.That(result, Is.EqualTo(expected));

I want to test that the output produced by calling DisplayFor is in the format specified by...

[DisplayFormat(DataFormatString = "{0:dd/MM/yy}")]
public DateTime? PriceDate { get; set; }

The code compiles but fails with a NullReferenceException at DisplayFor.

Can anyone help me make this work?

(Note: This is a trivial example of a larger problem)

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

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

发布评论

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

评论(2

穿越时光隧道 2024-12-02 08:49:29

步骤很长,所以我无法在这里写。我在我的博客上写过它:D

http:// thoai-nguyen.blogspot.com/2011/07/unit-test-displayformat-attribute.html

干杯

The steps are quite long so I couldn't write here. I wrote about it at my blog :D

http://thoai-nguyen.blogspot.com/2011/07/unit-test-displayformat-attribute.html

Cheers

饭团 2024-12-02 08:49:29

我使用以下代码来测试和验证 html 帮助程序。

验证是另一个例子。

请尝试以下操作:

    var sb = new StringBuilder();
    var context = new ViewContext();
    context.ViewData = new ViewDataDictionary(_testModel);
    context.Writer = new StringWriter(sb);
    var page = new ViewPage<TestModel>();
    var helper = new HtmlHelper<TestModel>(context, page);

    //Do your stuff here to exercise your helper

    //Following example contains two helpers that are being tested
    //A MyCustomBeginForm Helper and a OtherCoolHelperIMade Helper
    using(helper.MyCustomBeginForm("secretSauce"))
    {
       helper.ViewContext.Writer.WriteLine(helper.OtherCoolHelperIMade("bigMacSauce"));
    }
    //End Example

    //Get the results of all helpers
    var result = sb.ToString();

    //Asserts and string tests here for emitted HTML
    Assert.IsNotNullOrEmpty(result);

I use the following code to test and validate html helpers.

Validation is a another example.

Try the following:

    var sb = new StringBuilder();
    var context = new ViewContext();
    context.ViewData = new ViewDataDictionary(_testModel);
    context.Writer = new StringWriter(sb);
    var page = new ViewPage<TestModel>();
    var helper = new HtmlHelper<TestModel>(context, page);

    //Do your stuff here to exercise your helper

    //Following example contains two helpers that are being tested
    //A MyCustomBeginForm Helper and a OtherCoolHelperIMade Helper
    using(helper.MyCustomBeginForm("secretSauce"))
    {
       helper.ViewContext.Writer.WriteLine(helper.OtherCoolHelperIMade("bigMacSauce"));
    }
    //End Example

    //Get the results of all helpers
    var result = sb.ToString();

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