您在单元测试中检查 HttpVerbs 吗?

发布于 2024-07-25 06:06:21 字数 155 浏览 8 评论 0原文

在查看标准 ASP.MVC Web 项目模板附带的单元测试时,我注意到这些测试不会测试每个操作方法上是否设置了正确的 HttpVerbs 属性。

通过反射来测试这一点非常容易,但问题是它是否值得付出努力。 您是否在单元测试中检查 HttpVerbs,还是将其留给集成测试?

While looking at the unit tests that come with the standard ASP.MVC Web Project template, I noticed that these do not test whether or not a proper HttpVerbs attribute is set on each action method.

It's very easy to test this with reflection, but the question is whether or not it It's worth the effort. Do you check HttpVerbs in your unit test, or do you leave this up to Integration testing?

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

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

发布评论

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

评论(4

回眸一遍 2024-08-01 06:06:21

您还可以使用 MvcContrib.TestHelper 通过操作来测试路由。

    [TestMethod]
    public void ShouldConfirmRouteForNewObjectWithPost()
    {
        "~/Object/New".WithMethod(HttpVerbs.Post).ShouldMapTo<ObjectController>(x => x.New());
    }

You could also use the MvcContrib.TestHelper to test the route with actions.

    [TestMethod]
    public void ShouldConfirmRouteForNewObjectWithPost()
    {
        "~/Object/New".WithMethod(HttpVerbs.Post).ShouldMapTo<ObjectController>(x => x.New());
    }
雪化雨蝶 2024-08-01 06:06:21

如果其他人发现这个问题:我已经开始在单元测试中检查所有操作方法接受属性。 稍微反思一下就可以解决问题。 如果您也想这样做,这里有一些代码:

protected void CheckAcceptVerbs<TControllerType>(string methodName, HttpVerbs verbs)
{              
    CheckAcceptVerbs(methodName, typeof(TControllerType).GetMethod(methodName, BindingFlags.Public|BindingFlags.Instance,null,new Type[]{},null), verbs);
}

protected void CheckAcceptVerbs<TControllerType>(string methodName, Type[] ActionMethodParameterTypes, HttpVerbs verbs)
{
    CheckAcceptVerbs(methodName, typeof(TControllerType).GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance, null, ActionMethodParameterTypes, null), verbs);
}

private void CheckAcceptVerbs<TControllerType>(string methodName, MethodInfo actionMethod, HttpVerbs verbs)
{
    Assert.IsNotNull(actionMethod, "Could not find action method " + methodName);
    var attribute =
        (AcceptVerbsAttribute)
        actionMethod.GetCustomAttributes(false).FirstOrDefault(
            c => c.GetType() == typeof(AcceptVerbsAttribute));


    if (attribute == null)
    {
        Assert.AreEqual(HttpVerbs.Get, verbs);
        return;
    }

    Assert.IsTrue(HttpVerbsEnumToArray(verbs).IsEqualTo(attribute.Verbs));
}

第一个方法适用于不带参数的操作方法,第二个方法适用于带参数的操作方法。 您也可以直接使用第三种方法,但我将前两个重载编写为便利函数。

In case someone else finds this question: I've started checking all of my action method accept attributes in my unit tests. A bit of reflection does the trick just fine. Here's some code if you'd like to do this as well:

protected void CheckAcceptVerbs<TControllerType>(string methodName, HttpVerbs verbs)
{              
    CheckAcceptVerbs(methodName, typeof(TControllerType).GetMethod(methodName, BindingFlags.Public|BindingFlags.Instance,null,new Type[]{},null), verbs);
}

protected void CheckAcceptVerbs<TControllerType>(string methodName, Type[] ActionMethodParameterTypes, HttpVerbs verbs)
{
    CheckAcceptVerbs(methodName, typeof(TControllerType).GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance, null, ActionMethodParameterTypes, null), verbs);
}

private void CheckAcceptVerbs<TControllerType>(string methodName, MethodInfo actionMethod, HttpVerbs verbs)
{
    Assert.IsNotNull(actionMethod, "Could not find action method " + methodName);
    var attribute =
        (AcceptVerbsAttribute)
        actionMethod.GetCustomAttributes(false).FirstOrDefault(
            c => c.GetType() == typeof(AcceptVerbsAttribute));


    if (attribute == null)
    {
        Assert.AreEqual(HttpVerbs.Get, verbs);
        return;
    }

    Assert.IsTrue(HttpVerbsEnumToArray(verbs).IsEqualTo(attribute.Verbs));
}

The first method is for action methods without parameters, the second is for those with parameters. You can also just use the third method directly, but I wrote the first two overloads as convenience functions.

一身软味 2024-08-01 06:06:21

对于我来说,我总是在控制器中的每个操作上检查正确的 AcceptVerbsAttribute 。

As for me I'm always checking for a proper AcceptVerbsAttribute on each action in my controllers.

半山落雨半山空 2024-08-01 06:06:21

我不这么认为,但这只是因为直到你提到它之前我才想到这一点。 从现在开始我会的。

I don't but that's only because it hasn't crossed my mind until you mentioned it. I will from now on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文