使用 HtmlHelper 进行 DisplayFormat 单元测试
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
步骤很长,所以我无法在这里写。我在我的博客上写过它: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
我使用以下代码来测试和验证 html 帮助程序。
验证是另一个例子。
请尝试以下操作:
I use the following code to test and validate html helpers.
Validation is a another example.
Try the following: