如何不嘲讽我的单元测试操作使用的UpdateModel? [英] How do I Unit Test Actions without Mocking that use UpdateModel?

查看:178
本文介绍了如何不嘲讽我的单元测试操作使用的UpdateModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力通过斯科特Guthrie的上的 ASP.NET MVC Beta 1的。他在文中展示的方法的UpdateModel以及他们如何提高单元测试的改进。我已经重新创建一个类似的项目随时不过我运行一个包含的UpdateModel一个呼叫的UnitTest我收到ArgumentNullException命名controllerContext参数。

I have been working my way through Scott Guthrie's excellent post on ASP.NET MVC Beta 1. In it he shows the improvements made to the UpdateModel method and how they improve unit testing. I have recreated a similar project however anytime I run a UnitTest that contains a call to UpdateModel I receive an ArgumentNullException naming the controllerContext parameter.

下面是有关的位,首先是我的模型:

Here's the relevant bits, starting with my model:

public class Country {
  public Int32 ID { get; set; }
  public String Name { get; set; }
  public String Iso3166 { get; set; }
}

控制器动作:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Int32 id, FormCollection form)
{
  using ( ModelBindingDataContext db = new ModelBindingDataContext() ) {
    Country country = db.Countries.Where(c => c.CountryID == id).SingleOrDefault();

    try {
      UpdateModel(country, form);

      db.SubmitChanges();

      return RedirectToAction("Index");
    }
    catch {
      return View(country);
    }
  }
}

和最后的失败的我的单元测试:

And finally my unit test that's failing:

[TestMethod]
public void Edit()
{
  CountryController controller = new CountryController();
  FormCollection form = new FormCollection();
  form.Add("Name", "Canada");
  form.Add("Iso3166", "CA");

  var result = controller.Edit(2 /*Canada*/, form) as RedirectToRouteResult;

  Assert.IsNotNull(result, "Expected to be redirected on successful POST.");
  Assert.AreEqual("Show", result.RouteName, "Expected to redirect to the View action.");
}

ArgumentNullException 被调用的UpdateModel 消息值抛出不能为空参数名:controllerContext 。我假设某处的UpdateModel 要求 System.Web.Mvc.ControllerContext 这是不是$ P $在测试的执行期间psent

ArgumentNullException is thrown by the call to UpdateModel with the message "Value cannot be null. Parameter name: controllerContext". I'm assuming that somewhere the UpdateModel requires the System.Web.Mvc.ControllerContext which isn't present during execution of the test.

我也假设说我做得不对的地方,只需要在正确的方向。

I'm also assuming that I'm doing something wrong somewhere and just need to pointed in the right direction.

请帮助!

推荐答案

我不认为这是可以做到,因为TryUpdateModel,它的UpdateModel使用,引用ControllerContext从单元测试调用时,这是空。我使用RhinoMocks的嘲笑或存根由控制器所需的各种组件。

I don't think it can be done since TryUpdateModel, which UpdateModel uses, references the ControllerContext which is null when invoked from a unit test. I use RhinoMocks to mock or stub the various components needed by the controller.

var routeData = new RouteData();
var httpContext = MockRepository.GenerateStub<HttpContextBase>();
FormCollection formParameters = new FormCollection();

EventController controller = new EventController();
ControllerContext controllerContext = 
    MockRepository.GenerateStub<ControllerContext>( httpContext,
                                                    routeData,
                                                    controller );
controller.ControllerContext = controllerContext;

ViewResult result = controller.Create( formParameters ) as ViewResult;

Assert.AreEqual( "Event", result.Values["controller"] );
Assert.AreEqual( "Show", result.Values["action"] );
Assert.AreEqual( 0, result.Values["id"] );

下面是从WWW的Controller.cs源$ C ​​$ cplex.com / ASPNET的相关位:

Here's the relevant bit from the Controller.cs source on www.codeplex.com/aspnet:

protected internal bool TryUpdateModel<TModel>( ... ) where TModel : class
{

     ....

    ModelBindingContext bindingContext =
           new ModelBindingContext( ControllerContext,
                                    valueProvider,
                                    typeof(TModel),
                                    prefix,
                                    () => model,
                                    ModelState,
                                    propertyFilter );

     ...
}

这篇关于如何不嘲讽我的单元测试操作使用的UpdateModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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