Last post Dec 14, 2007 03:46 PM by jcteague
Dec 13, 2007 03:24 AM|NeoAdroit|LINK
I guess this is more a testing issue than MVC, but i am trying to test a MVC controller .
How do I get access to web.config while invoking my controller from my testcase ? I created a instance by controller by default constructor, Is there any special way to create my controller object ?
Also, when controller calls my stubbed out TestViewEngine, controllerContext is empty, which again goes back to the way, my controller instance is created.
Any one have other ideas to create a controller from a test case ?
Dec 13, 2007 08:54 AM|haacked|LINK
I wrote a brief guide to testing controllers.
Hopefully that should help.
Dec 13, 2007 04:16 PM|NeoAdroit|LINK
I did find way to it last night! Of course just minutes after posting the question out here.
One thing, i sure figured out Rhino Mocks for mocking my controller context and create a Dummy IViewFactory, but what about acessing web.config file from with the controller.
For starters, lets stay my controller class has code to access database and needs connection string from web.config ? How do i test the code for accessing the web,config file ?
Dec 13, 2007 05:37 PM|haacked|LINK
Depending on your unit testing platform, I usually copy my Web.config to my test project and rename it App.config. This works really well with MbUnit. I remove all the web-specific settings from the unit test App.config file.
If you need to swap out config files for your unit test to try multiple different settings, well you're on your own. I've been trying to find a way to do that myself. My recommendation in that case is add a small layer between your controller and your config
Dec 13, 2007 09:03 PM|NeoAdroit|LINK
Okay, copying the configuration files, and messing with them, hmm...that for some reason doesnt appeal to me. Wondering if System.Configuration can help. Im thinking in terms of usign ConfigurationManager to load up form web.config file, as part of the test
start up. Havent tried it yet, but i guess its worth try. Any comments ?
Dec 14, 2007 08:40 AM|haacked|LINK
Seems like a lot of work. You could always have a pre build step that copies web.config and renames it to App.config. I'd give that a shot first. :)
Dec 14, 2007 03:46 PM|jcteague|LINK
My approach isn't as complete as Phil's, but the reality is that you usually don't need ALL of the configuration from the web.config. I manually add the items I need to my testing app.config. It's lower tech, but usually my config stuff doesn't change