一个单位应该如何测试一个.NET MVC控制器? [英] How should one unit test a .NET MVC controller?

查看:174
本文介绍了一个单位应该如何测试一个.NET MVC控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找关于.NET MVC控制器的有效的单元测试的建议。

I'm looking for advice regarding effective unit testing of .NET mvc controllers.

我在哪里工作,许多这样的测试,使用起订量嘲笑数据层和断言,某些数据层的方法被调用。这似乎并不对我有用,因为它本质上验证的实施并没有改变,而不是测试的API。

Where I work, many such tests use moq to mock the data layer and to assert that certain data-layer methods are called. This doesn't seem useful to me, since it essentially verifies that the implementation has not changed rather than testing the API.

我也读条建议之类的东西检查视图模型的返回类型是正确的。我可以看到,提供了一定的价值,但仅凭它似乎并不值得写嘲讽code(我们的应用程序的数据模型是非常大的,复杂的)多行的努力。

I've also read articles recommending things like checking that the type of view model returned is correct. I can see that providing some value, but alone it doesn't seem to merit the effort of writing many lines of mocking code (our application's data model is very large and complex).

任何人都可以提出一些更好的方法来控制单元测试或解释为何上述方法是有效的/有用吗?

Can anyone suggest some better approaches to controller unit testing or explain why the above approaches are valid/useful?

谢谢!

推荐答案

一个控制器单元测试应该在动作方法测试code算法,而不是在你的数据层。这是一个原因嘲笑这些数据服务。控制器期望从库/服务/等接收一定值,并且当它从它们那里接收不同的信息采取不同的行动。

A controller unit test should test the code algorithms in your action methods, not in your data layer. This is one reason to mock those data services. The controller expects to receive certain values from repositories / services / etc, and to act differently when it receives different information from them.

您编写单元测试断言控制器在非常特殊的情况/情况非常特殊的方式表现。您的数据层是提供这些情况给控制器/ action方法应用的一块。声称服务的方法是由控制器进行调用是有价值的,因为你可以肯定,该控制器会从另一个地方的信息。

You write unit tests to assert the controller behaves in very specific ways in very specific scenarios / circumstances. Your data layer is one piece of the app that provides those circumstances to the controller / action methods. Asserting that a service method was called by the controller is valuable because you can be certain that the controller gets the information from another place.

检查返回的视图模型的类型是有价值的,因为,如果返回错误类型的视图模型中,MVC将抛出一个运行时异常。您可以通过运行单元测试生产发生prevent这一点。如果测试失败,则该视图可能扔在生产异常。

Checking the type of the viewmodel returned is valuable because, if the wrong type of viewmodel is returned, MVC will throw a runtime exception. You can prevent this from happening in production by running a unit test. If the test fails, then the view may throw an exception in production.

,因为它们使得重构更加容易单元测试可以是有价值的。你可以改变的执行情况,并断言的行为仍然是确保所有的单元测试通过的相同。

Unit tests can be valuable because they make refactoring much easier. You can change the implementation, and assert that the behavior is still the same by making sure all of the unit tests pass.

答案评论#1

如果改变一个方法下的测试需要改变/去除低层嘲笑方法的实现,那么单元测试也必须改变。但是,这不应该经常,你可能认为发生了。

If changing the implementation of a method-under-test calls for the change / removal of a lower-layer mocked method, then the unit test must also change. However, this shouldn't happen as often as you may think.

典型的红 - 绿 - 重构的工作流程要求编写之前,单元测试的 写他们测试的方法。 (这意味着短暂的时间量,你的测试code将无法编译,而且也是许多年轻/经验的开发人员很难采用红,绿,重构。)

The typical red-green-refactor workflow calls for writing your unit tests before writing the methods they test. (This means for a brief amount of time, your test code won't compile, and is why many young / inexperienced developers have difficulty adopting red green refactor.)

如果您首先编写单元测试,你会来的地方,你知道控制器需要从较低层获取信息的一个点。你怎么能肯定它会尝试获取这些信息?由嘲笑出较低层的方法,提供的信息,并声称下层方法由控制器调用。

If you write your unit tests first, you will come to a point where you know the controller needs to get information from a lower layer. How can you be certain it tries to get that information? By mocking out the lower layer method that provides the information, and asserting that the lower-layer method is invoked by the controller.

当我用了我可能说错转变的实现。当一个控制器的操作方法和放大器;对应的单元测试必须改变更改或删除一个模拟的方法,你真的改变控制器的行为。重构,根据定义,意味着改变实施而不改变整体行为和预期结果。

I may have misspoke when I used the term "changing implementation." When a controller's action method & corresponding unit test must be altered to change or remove a mocked method, you are really changing the behavior of the controller. Refactoring, by definition, means changing the implementation without altering the overall behavior and expected results.

红 - 绿 - 重构是一种质量保证方法,帮助prevent错误和放大器;在code缺陷之前,他们曾经出现。通常开发商更改实施它们出现后删除错误。因此,要重申的是,你所担心的情况下,不应该经常你认为发生。

Red-green-refactor is a Quality Assurance approach that helps prevent bugs & defects in code before they ever appear. Typically developers change implementation to remove bugs after they appear. So to reiterate, the cases you are worried about should not happen as often as you think.

这篇关于一个单位应该如何测试一个.NET MVC控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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