2

I'm new at this TDD thing but making a serious effort, so I'm hoping to get some feedback here.

I created a little web service to minify JavaScript, and everything was nice, with all my tests passing. Then I noticed a bug: if I tried to minify alert('<script>');, it would throw a HttpRequestValidationException.

So that's easy enough to fix. I'll just add [AllowHtml] to my controller. But what would be a good way to unit test that this doesn't happen in the future?

The following was my first thought:

[TestMethod]
public void Minify_DoesntChokeOnHtml()
{
    try
    {
        using (var controller = ServiceLocator.Current.GetInstance<MinifyController>())
        {
            return controller.Minify("alert('<script></script>');");
        }
    }
    catch (HttpRequestValidationException)
    {
        Assert.Fail("Request validation prevented HTML from existing inside the JavaScript.");
    }
}

However, this doesn't work since I am just getting a controller instance and running methods on it, instead of firing up the whole ASP.NET pipeline.

What would be a good unit test for this? Maybe reflector on the controller method to see if the [AllowHtml] attribute is present? That seems very structural, and unlikely to survive a refactoring; something functional might make more sense. Any ideas?

4

1 回答 1

1

你只有两个选择:

第一的

编写包含 MVC in-proc 或使用浏览器(例如使用 Watin)运行的集成测试,它将涵盖您的场景。

第二

编写单元测试来检查方法是否被标记为需要的属性。

我会选择第一个选项。

于 2011-02-02T11:23:57.253 回答