views:

360

answers:

2

Edit

I noticed that the Object Test Bench is a good example of what I'm looking for, but it doesn't seem to work with ASP.net. I always get an error that an instance could not be created.


Is there a way to test your functions in ASP.net without needing to create a web page and click on a button. For example, if I create a controller that returns a JSON result, I end up having to create a web page, create some javascript to call the method, starting the website and then clicking the button.

What would be really great is to just type the name of the command into the Immediate Window and just provide the parameters at that point to see how they behave, but all my efforts there seem to have a problem by getting messages like The expression cannot be evaluated while in run mode. or whatever (I think there is another message for interactive mode as well).

Are there any tricks to just run a method and see the results without needing to create any UI to make the calls?

+2  A: 

Put all your non-presentation code (which excludes serializing to JSON—i.e., put all the code that goes before serialization) in a class library and link to the DLL directly from the unit tests.

Also, to get any expression to be evaluated in the debugger, you have to be in break mode (hit a breakpoint or press the stop button, which has a stop-square on it). Run mode usually means that the debugger hasn't paused execution so the stack pointer is constantly going up and down and changing scopes.

Mark Cidade
If you make use of the HttpContext your code won't work that well if you test it outside of a website. I've tried including the final compiled DLLs in a secondary project (like a Console application), but haven't had much success from that either.
Hugoware
refactor your code so that it doesn't depend on HttpContext. Anything you need from the context, extract it yourself and pass it. You can also use HttpContextBase from .NET 3.5 SP1: http://msdn.microsoft.com/en-us/library/system.web.httpcontextbase.aspx
Mark Cidade
A: 

There use to be a project call NUnitASP that's now dead. But the last version may help you in some ways.

Your next choice is to use the Unit Testing functions in Visual Studio. There were many features that were only available in the more expensive "Visual Studio Team Suite Test Edition" that are now part of "Visual Studio Professional".

But if you have deep pockets and the problem is worth enough to you, then "Visual Studio Team Suite Test Edition 2008" is probably the best you can do right now.

Check these videos to get some idea http://www.asp.net/learn/vsts-videos/

kervin