【发布时间】:2012-01-20 10:42:57
【问题描述】:
所以我正在创建一个主要基于这个项目http://www.codeproject.com/KB/aspnet/aspnet_mvc_restapi.aspx 的自定义 ActionFilter。
我想要一个自定义操作过滤器,它使用 http 接受标头返回 JSON 或 Xml。典型的控制器操作如下所示:
[AcceptVerbs(HttpVerbs.Get)]
[AcceptTypesAttribute(HttpContentTypes.Json, HttpContentTypes.Xml)]
public ActionResult Index()
{
var articles = Service.GetRecentArticles();
return View(articles);
}
自定义过滤器覆盖 OnActionExecuted 并将对象(在本示例文章中)序列化为 JSON 或 Xml。
我的问题是:我该如何测试?
- 我要编写哪些测试?我是一名 TDD 新手,不能 100% 确定我应该测试什么,不测试什么。我想出了
AcceptsTypeFilterJson_RequestHeaderAcceptsJson_ReturnsJson()、AcceptsTypeFilterXml_RequestHeaderAcceptsXml_ReturnsXml()和AcceptsTypeFilter_AcceptsHeaderMismatch_ReturnsError406()。 - 如何在 MVC 中测试正在测试 Http Accept Headers 的 ActionFilter?
谢谢。
【问题讨论】:
-
未来的读者可能想看看这个答案:stackoverflow.com/questions/36629391/…
标签: asp.net-mvc unit-testing tdd actionfilterattribute