简体   繁体   English

使用ASP.NET MVC模型绑定程序进行单元测试

[英]Unit testing with ASP.NET MVC model binders

I was wondering the best practice for unit-testing controller actions that utilize model binding. 我想知道利用模型绑定进行单元测试控制器动作的最佳实践。

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult AddProduct(Product product)
{
}

I was wondering how you invoke the controller's method for unit testing. 我想知道您如何调用控制器的方法进行单元测试。 If you try something like this... 如果您尝试这样的事情...

public void Catalog_AddProduct()
{
    CatalogController controller = new CatalogController();
    // some mocking for controller context, setting form values etc...
    controller.AddProduct(// ?);
}

Some might suggest removing Product as a parameter but, I also have another AddProduct controller action that is used for just HTTP-Gets. 有人可能建议删除Product作为参数,但我还有另一个AddProduct控制器操作,仅用于HTTP-Gets。 The only solution I could think of is maybe accepting a namevalue collection (form data) and just using UpdateModel/TryUpdateModel. 我唯一想到的解决方案可能是接受namevalue集合(表单数据)并仅使用UpdateModel / TryUpdateModel。

I also want to test the model binding itself is working properly so I'd like to put the responsibility of creating the new product to the model binder. 我还想测试模型绑定本身是否正常工作,所以我想将创建新产品的责任归咎于模型绑定器。

I'm not sure I understand the problem, why can't you just do this: 我不确定我是否理解问题,为什么你不能这样做:

[TestMethod()]
public void AddProductTest()
{
    CatalogController target = new CatalogController(/*testing variables*/);
    target.AddProduct(new Product { /* product details for testing */ });

    // Test the results
}

Though I think perhaps I'm not understanding the problem. 虽然我认为也许我不了解这个问题。 Using the form post variables is a good approach however, this will work really well when you need to do edits of the product and after a while you might find it's much easier to have all your actions take the form post variables and update your model. 使用表单发布变量是一种很好的方法,但是,当您需要对产品进行编辑时,这确实会很好地工作。一段时间后,您可能会发现,所有操作都采取表单发布变量并更新模型要容易得多。 One thing worth pointing out with the TryUpdateModel and UpdateModel is that we have run into a bug with the Entity Framework, if you try to update an entitiy framework model that is complex it can sometimes throw exceptions. 使用TryUpdateModel和UpdateModel值得一提的是,我们在使用实体框架时遇到了一个错误,如果您尝试更新复杂的实体框架模型,则有时可能会引发异常。 But it's really easy to write your own model updater as we've done. 但是,像我们所做的那样,编写自己的模型更新器真的很容易。

EDIT: 编辑:

I'm not sure you'll be able to, or that you need to, test the model binding itself. 我不确定您是否能够或是否需要测试模型绑定本身。 The model binding is part of the MVC framework and outside the scope of the test for the controller, I wouldn't concern myself with it and assume that it will work within the context of your test. 模型绑定是MVC框架的一部分,并且不在控制器的测试范围内,我不会担心它,并假定它会在您的测试环境中工作。

If you really need to test the model binding, the only way that I know is to pass in the form post variables and then use the TryUpdateModel method. 如果您确实需要测试模型绑定,那么我知道的唯一方法是传递表单发布变量,然后使用TryUpdateModel方法。

现在,您刚刚向控制器提供了ValueProvider。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM