Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net MVC functional testing

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!

like image 882
Cory Avatar asked Jul 12 '11 17:07

Cory


1 Answers

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.

like image 69
Brett Allred Avatar answered Oct 17 '22 12:10

Brett Allred