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 ?
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 ?
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
file.
Phil Haack (http://haacked.com/)
Senior Program Manager, Microsoft
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 ?
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
often enough.
NeoAdroit
Member
102 Points
28 Posts
Testing Controllers
Dec 13, 2007 02:24 AM|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 ?
- neo
testing controllers
Haacked
Contributor
6901 Points
412 Posts
Re: Testing Controllers
Dec 13, 2007 07:54 AM|LINK
I wrote a brief guide to testing controllers. http://haacked.com/archive/2007/12/09/writing-unit-tests-for-controller-actions.aspx
Hopefully that should help.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
NeoAdroit
Member
102 Points
28 Posts
Re: Testing Controllers
Dec 13, 2007 03:16 PM|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 ?
Neo.
Haacked
Contributor
6901 Points
412 Posts
Re: Testing Controllers
Dec 13, 2007 04:37 PM|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 file.
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
NeoAdroit
Member
102 Points
28 Posts
Re: Testing Controllers
Dec 13, 2007 08:03 PM|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 ?
Haacked
Contributor
6901 Points
412 Posts
Re: Testing Controllers
Dec 14, 2007 07:40 AM|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. :)
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
jcteague
Member
211 Points
45 Posts
Re: Testing Controllers
Dec 14, 2007 02:46 PM|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 often enough.