.net MVC 功能测试

发布于 2024-11-19 16:50:14 字数 272 浏览 2 评论 0原文

如何对 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情痴 2024-11-26 16:50:14

请原谅我对 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.

仅此而已 2024-11-26 16:50:14

如果您确实想测试功能,而不是标准的“单元”测试,您有几种选择。

  1. 发挥创意并使用 MS 测试来设置更多基于功能的测试。
  2. 使用 BDD 类型的框架(specFlow?)
  3. 使用更具功能性的测试框架,例如 Selenium

如果您的目标是针对更自动化的 UAT 进行功能测试,那么您还可以使用 FIT 框架(和 Fitnesse)等项目。

这些是 Rails 中工具和框架的直接模拟吗?不,但是 .NET 不是 Rails。

If you truly want to test functionality, as opposed to standard "unit" testing, you have a few choices.

  1. Get creative and use MS test to set up more function based tests.
  2. use a BDD type of framework (specFlow?)
  3. use a more functional test framework, like Selenium

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.

十年九夏 2024-11-26 16:50:14

对 MVC 应用程序进行单元测试的最佳方法是在解决方案中添加新的单元测试项目。只需右键单击项目解决方案,点击添加然后添加->测试项目。

您将看到一个测试类,其中的方法具有 [TestMethod] 属性。您将在这些方法中编写单元测试逻辑。

在测试项目中添加要执行单元测试的 MVC 项目的新引用 .dll。这将使测试项目能够发现主项目的实际控制器操作。

添加对 System.Web.MVC 命名空间的引用。

请参考下面的测试方法示例:

[TestMethod]
public void DisplayCustomer()
{
   CustomerController obj = new CustomerController();
   var testResult = obj.DisplayCustomer();
   Assert.AreEqual("DisplayCustomer",testResult.ViewName);
}

构建并运行项目以在“测试结果”窗口中查看通过/失败的测试

希望这会有所帮助!

谢谢。

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:

[TestMethod]
public void DisplayCustomer()
{
   CustomerController obj = new CustomerController();
   var testResult = obj.DisplayCustomer();
   Assert.AreEqual("DisplayCustomer",testResult.ViewName);
}

Build and run the project to see pass/failed tests in the Test Results window

Hope this helps !

Thanks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文