.net MVC 功能测试
如何对 .net mvc 控制器/操作进行功能测试。
我刚刚经历了多年的 Rails 开发,开始从事 .net mvc 的工作。这很痛苦,但我希望这很大程度上只是学习曲线。
对我而言,不太明显的是 .NET 世界中 Rails 功能和集成测试的类比。 Rails 使功能测试变得如此明显和简单,如果您不花时间进行覆盖,那就太愚蠢了。对于.NET - 我还没有让Google 给出一个看起来值得的结果。我希望是我刚刚起步的 .net 状态阻止我输入正确的搜索词或错过某些范例。
帮助!
How do I functional test a .net mvc controller/action.
I just came from many years of Rails development into a gig where I'm hacking on .net mvc. It hurts but I'm hoping that a large part of this is just the learning curve.
What's not immediately obvious to me is an analog for Rails functional and integration testing in the .NET world. Rails makes functional testing so obvious and simple that you'd be foolish not to take the time to get coverage in place. With .NET - I've yet to get Google to yield a single result that seems worthwhile. I'm hoping it's my nascent .net status that's just keeping me from entering the right search terms or missing some paradigm.
HELP!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请原谅我对 Rails 的缺乏了解,并告诉您您已经了解的 Rails 知识,但我希望将这些内容以比较的格式提供给其他用户。
Rails 有 3 种类型的测试:单元测试、功能测试和集成测试。
单元测试测试您的模型
控制器的功能测试
集成测试用于测试控制器操作之间的流程
这种范例似乎是从一开始就通过 Rails 进行教授的。
然而,在 .NET MVC 世界中,测试方法的布局并不像 Rails 中那样。
许多开发人员会在他们的控制器上编写单元测试,就像在 Rails 中的模型上编写单元测试一样。您基本上调用一个方法(控制器操作)并从该方法返回一个对象。然后您可以断言它具有您期望的值。这样做是一件很痛苦的事情,因为你必须模拟很多废话(HttpContext 等)。另外,这不是功能测试。您仅测试一种方法,而不是测试应用程序的功能。
在 Rails 中,您并不是在控制器上编写单元测试,而是实际上发出 Web 请求并获得 Web 响应。您可以检查状态代码、cookie 等。您正在从头到尾地测试系统。
在 .NET MVC 中,您可以通过几种方法来完成此操作
1) Steven Sanderson 有一个小工具可以帮助您完成此操作。
http://blog.stevensanderson.com/ 2009/06/11/integration-testing-your-aspnet-mvc-application/
当我第一次看到这个并开始使用它时,我认为它很棒,但我遇到了问题。
2) RestSharp + NUnit - 这是我目前进行此类测试的偏好。它允许您将网络请求放在一起并非常容易地获得响应。通过一些通用方法,您可以使用 RestSharp 快速移动。 NUnit 将为您提供所需的断言。我只是向本地 IIS 服务器发出请求,并断言我在响应中期望的不同项目。您实际上无法像在 Rails 中那样测试分配给视图的模型,但这对我来说不是问题。
如果您习惯使用 RSpec,那么您可以使用 SpecFlow,它应该类似。
Rails 将测试直接构建到框架中,并且它是一等公民。遗憾的是,.NET MVC 中并非如此。
希望有帮助。
Forgive me for my lack of knowledge of Rails, and for telling you what you already know about rails, but I hope to put things in a comparison format for other users.
Rails has 3 types of tests Unit, Functional, and Integration.
Unit Tests test your Models
Functional Tests for your Controllers
Integration Tests for testing the Flow between Controller Actions
This paradigm seems to be taught from the beginning with rails.
However in the .NET MVC world the methods of testing aren't laid out like they are in Rails.
Many developers will write Unit Tests on their controllers in the way you write a Unit Test on your Models in rails. You basically call a method (Controller Action) and get an object back from the method. You can then assert it has the values you expect. Doing this is a pain in the butt because you have to mock so much crap (HttpContext etc). Plus, it isn't a functional test. You are only testing the one method as opposed to testing the functionality of the application.
In Rails you aren't writing a Unit Test on your controller, you are actually making a web request and you get a web response. You can check the status code, cookies, etc. You are testing the system from end to end.
There are a few ways you can do this in .NET MVC
1) Steven Sanderson has a little tool to help with this.
http://blog.stevensanderson.com/2009/06/11/integration-testing-your-aspnet-mvc-application/
When I first saw this and started using it I thought it was awesome, but I ran into problems.
2) RestSharp + NUnit - This is my current preference for doing these kinds of tests. It allows you to put a web request together and get a response pretty easy. With a few generic methods you can move pretty fast with restsharp. NUnit will give you the assertions you need. I just make the request against my local IIS server and assert the different items I expect in the response. You won't really be able to test which model is assigned to the view like you can in rails but that hasn't been a problem for me.
If you are used to RSpec then you can get SpecFlow which should similar.
Rails builds testing right into the framework and it is a first class citizen. Its too bad it isn't this way in .NET MVC.
Hope that helps.
如果您确实想测试功能,而不是标准的“单元”测试,您有几种选择。
如果您的目标是针对更自动化的 UAT 进行功能测试,那么您还可以使用 FIT 框架(和 Fitnesse)等项目。
这些是 Rails 中工具和框架的直接模拟吗?不,但是 .NET 不是 Rails。
If you truly want to test functionality, as opposed to standard "unit" testing, you have a few choices.
If you are aiming the function testing towards more automated UAT, you also have items like the FIT Framework (and Fitnesse).
Are these a direct analog to the tooling and frameworks in Rails? No, but .NET is not Rails.
对 MVC 应用程序进行单元测试的最佳方法是在解决方案中添加新的单元测试项目。只需右键单击项目解决方案,点击添加然后添加->测试项目。
您将看到一个测试类,其中的方法具有 [TestMethod] 属性。您将在这些方法中编写单元测试逻辑。
在测试项目中添加要执行单元测试的 MVC 项目的新引用 .dll。这将使测试项目能够发现主项目的实际控制器操作。
添加对 System.Web.MVC 命名空间的引用。
请参考下面的测试方法示例:
构建并运行项目以在“测试结果”窗口中查看通过/失败的测试
希望这会有所帮助!
谢谢。
The best way to unit test MVC application is to add a new Unit Test Project inside the solution. Just right click on the project solution, click on add then add -> test project.
You will see a test class which will have methods having [TestMethod] attribute on top of them. You will be writing your unit testing logic in these methods.
Inside the test project add new reference .dll of the MVC project on which the unit testing is to be carried out. This will enable the test project to discover the actual controller actions of the main project.
Add a reference to System.Web.MVC namespace.
Please refer to a sample of a test method below:
Build and run the project to see pass/failed tests in the Test Results window
Hope this helps !
Thanks.