.net MVC functional testing

How can I check the operation of the .net mvc / action controller.

I just came from years of Rails development to a concert where I hack .net mvc. It hurts me, but I hope that a significant part of this will only be a learning curve.

What is not immediately obvious to me is an analogue for functional and integration testing of Rails in the .NET world. Rails makes functional testing so obvious and simple that you would be foolish not to waste time lighting. With .NET - I haven't gotten Google yet to get the only result that seems appropriate. I hope this is my nascent .net status, which simply prevents me from entering the correct search conditions or if there is no paradigm.

HELP!

+6
source share
3 answers

Forgive me for not knowing Rails and 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 Unit, Functional, and Integration tests.

Unit tests test your models

Functional tests for your controllers

Integration Tests for testing flow between controller actions

This paradigm seems to teach from the very beginning with rails.

However, in the world of .NET MVC, test methods are not laid out, as in Rails.

Many developers will write Unit Tests on their controllers the way you write Unit Test on their models in rails. You basically call the method (controller action) and return the object back from the method. You can then claim that it has the meanings that you expect. This is a pain in the ass because you have to mock so much shit (HttpContext, etc.). In addition, this is not a functional test. You are testing only one method, and not testing the functionality of the application.

In Rails, you are not writing Unit Test on your controller, you are actually making a web request and getting a web response. You can check the status code, cookies, etc. You are testing the system from end to end.

There are several ways to do this in .NET MVC

1) Stephen Sanderson has a small 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 is my current preference for conducting such tests. This allows you to put a web request together and get a response quite easily. With a few common methods, you can navigate quickly with restsharp. NUnit will provide you with the statements you need. I just make a request against my local IIS server and assert the various elements that I expect in the response. You really cannot check which model is assigned to represent how you can on rails, but that was not a problem for me.

If you use RSpec, you can get SpecFlow, which should be similar.

Rails builds testing directly in the framework and is a first-class citizen. Too bad that is not the case in .NET MVC.

Hope this helps.

+3
source

If you really want to test functionality, unlike standard unit testing, you have several options.

  • Get creative and use the MS test to set up more feature-based tests.
  • use the BDD framework structure (specFlow?)
  • use a more functional test structure like Selenium

If you are aiming feature testing for a more automated UAT, you also have elements such as the FIT Framework (and Fitnesse).

Are they a direct analogue of tools and frameworks in Rails? No, but .NET is not Rails.

0
source

The best way to use the unit test MVC application is to add a new Unit Test Project inside the solution. Just right-click on the design solution, click "Add", then "Add" → "Project".

You will see a test class that will have methods that have the [TestMethod] attribute. You will write device testing logic in these methods.

Inside the test project, add a new .dll link to the MVC project, on which unit testing should be performed. This will allow the test project to detect the actual actions of the controller of the main project.

Add a reference to the System.Web.MVC namespace.

Please refer to the sample test method below:

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

Create and run the project to see the failed / failed tests in the test results window

Hope this helps!

Thanks.

0
source

Source: https://habr.com/ru/post/892601/


All Articles