mvccontrib测试助手和验证HTTP POST路线和参数 [英] mvccontrib test helper and verifying http post routes and parameters

查看:208
本文介绍了mvccontrib测试助手和验证HTTP POST路线和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Asp.net MVC应用程序,我有一个控制器上有两个方法,一个是当用户第一次到达的观点再一个,当他们提交称视图的形式。

In my Asp.net MVC app, I have two methods on a controller, one for when the user first arrives on the view and then one when they submit the form on said view.

public ActionResult Foo() {}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Foo(string id, Account accountToFoo) {}

在第二个动作,有这么的组装我作用于该帐户对象的自定义模型绑定,虽然这真的并不重要。这一切都在测试工作正常本地的服务器上。

In the second action, there's a custom model binder that's assembling the account object that I'm acting on, though that's really not important. This all works fine in testing locally on a server.

我们尽量pretty良好编写单元测试来测试我们所有的不同意见都正确得到路由到,包括那些HTTP POST。要做到这一点,我们一直在使用mvccontrib的测试帮手。

We try to be pretty good about writing unit tests to test all our different views are properly getting routed to, including those that are HTTP POST. To do so, we've been using mvccontrib's test helper.

测试得到过超级简单

"~/account/foo/myusername".
       Route().
       ShouldMapTo<AccountController>(c => c.Foo("myusername"));

我的问题是在测试邮路,我怎么写我会使用验证后接收精确值的lambda,类似于上面的GET测试?

My question is in testing POST routes, how do I write the lambda that I would use to verify the post is receiving accurate values, similar to the GET test above?

对于POST,它看起来是这样的:

For a POST, it looks something like:

"~/account/foo".
         WithMethod(HttpVerbs.Post).
         ShouldMapTo<AccountController>(a => something_something);

这是我的lambda是我遇到的麻烦的something_something部分。使用任意值不工作(A => a.Foo(0,新的帐户())。我怎么会指定的预期值作为测试的一部分?

It's the something_something portion of my lambda that I'm having trouble with. Using arbitrary values doesn't work ("a => a.Foo(0, new Account()"). How would I specify the expected values as part of the test?

修改我希望有一个类似于起订量有语句,如foo.Setup(S => s.Foo(It.IsAny(),It.Is(lambda表达式我的方式=> I> 32))等连我必须明确提供值,这是可行的 - 我似乎无法神交所需的结构来传递这些明确值

EDIT I was hoping there was something akin to the way Moq has lambdas for statements such as foo.Setup(s => s.Foo(It.IsAny(), It.Is(i => i > 32)) and so on. Even I have to explicitly supply the values, that's workable--I just can't seem to grok the desired structure to pass those explicit values.

推荐答案

下面是一个例子。假设你有以下动作:

Here's an example. Assuming you have the following action:

public AccountController : Controller
{
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Foo(string id) 
    {
        return View();
    }
}

和注册以下路线:

RouteTable.Routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "home", action = "index", id = "" }
);

您可以测试它是这样的:

You could test it like this:

var routeData = "~/account/foo".WithMethod(HttpVerbs.Post);
routeData.Values["id"] = "123";
routeData.ShouldMapTo<AccountController>(c => c.Foo("123"));

有些调整可能是必要的,包括第二个帐户有​​参数。

这篇关于mvccontrib测试助手和验证HTTP POST路线和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆