0

I am trying to define some acceptance tests on ASP.NET MVC controllers. My tests run in the context of a separate testing application as you'd probably expect. My MVC controllers need to run in the context of the application - i.e. over top of the web.config defined for the MVC project. The project is using Castle Windsor to instantiate the dependencies in the live project.

I'd like my tests to instantiate the MVC controllers directly rather than running as an HTTP client.

We've done this in the past by copying the web.config from the web project over to the test project, but over time this has become a maintenance nightmare having to track through chains of dependencies every time a change is made.

Is there a more suitable approach to testing this stack without

  1. Copying the web config which causes a maintenance nightmare
  2. Having our acceptance tests run in a client/server fashion over http
BenAlabaster
  • 39,070
  • 21
  • 110
  • 151
  • "I'd like my tests to instantiate the MVC controllers directly" This is a major issue, because as you already know the MVC Controller is tightly coupled to the MVC framework. This is most likely why Freeman in his excellent book starts out early with Unit Testing and shows developers how to use Interfaces for controller parameters. When the code does that you can inject anything you want... When it doesn't you only have a client side approach... Other consideration could be to not have any logic in the controller at all. They only new up ViewModels and pass them to view. – JWP Feb 18 '15 at 20:24

1 Answers1

0

In the past, I've exposed my application settings through an interface. Let's me mock them out during testing and eliminates the need to copy these settings around between the web project and the test project.

For unit testing MVC controllers, you may have to use some helper libraries if you utilize HTTP-context based objects like Request, Repsonse, Session, etc... I've used MVC Contrib in the past to do this sort of thing.

PatrickSteele
  • 14,489
  • 2
  • 51
  • 54