How can I unit test my ASP.NET MVC controller that uses FormsAuthentication?


Answers

Unit Testing ASP.Net MVC 4 apps thoroughly

  1. Making sure a view exists
  2. http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx/
  3. http://blog.davidebbo.com/2011/06/unit-test-your-mvc-views-using-razor.html
  4. see below
  5. How can I unit test my ASP.NET MVC controller that uses FormsAuthentication?
  6. How-to test action filters in ASP.NET MVC?

no. 4: This is a hard question. How does one test anything thoroughly? Personally, I don't really test the views, other than with the 3 major browsers and my two eyes, as it's hard to test a website and all it's components without actually using it. You have JavaScript firing, CSS stylizing, and it looks different across different browsers. So, to me, it seems like testing the view that thoroughly is a minor part of the overall usability of your site. If you're developing a simple table based report of financial data, test that data hard. If your view is a the base for a fancy Ajax site, maybe don't test the HTML so much as the experience. I know it's not an easy, cut-and-dry answer, but the acceptable level of coverage always involves trade offs.

Question

I'm working with a ASP.NET MVC solution in a test driven manner and I want to login a user to my application using forms authentication. The code I would like to end up with in the controller looks something like this:

FormsAuthentication.SetAuthCookie(userName, false);

My question is how do I write a test to justify this code?

Is there a way to check that the SetAuthCookie method was called with the correct parameters?

Is there any way of injecting a fake/mock FormsAuthentication?




Tags