Hi everyone!
I've noticed while developing e-commerce oriented web sites (ASP.NET 2.0 of course) that it's hard to develop and test pages that relies a lot on context (meaning past actions or data that accumulate in one's session). What I mean is that, for instance, in our e-commerce web site, one has to log in, add products to the shopping cart, check out, choose a shipping address, choose a shipping method, enter credit card information, etc. So when I am developing the pages near the end of the process (e.g. the credit card info entry page), I have to go through EVERYTHING just to get to the page that I am interested in testing/developing. Doing this repetitively is tedious and boring.
What is your strategy or approach in developing pages like this?
The approach I've taken for now is to put a conditional #define and have code sections marked with #ifdef ... #endif. So if I normally only do something like this:
1234567
protected void Page_Init(object sender, EventArgs e){ if (!SessionHelper.UserIsLoggedIn()) { Response.Redirect("loginpage.aspx"); } ProcessMyEventsHere();}
I replace that with:
1234567891011121314151617181920
protected void Page_Init(object sender, EventArgs e){ #ifdef TESTING_ONLY if (!SessionHelper.UserIsLoggedIn()) { SetUpContextActions(); } #endif if (!SessionHelper.UserIsLoggedIn()) { Response.Redirect("loginpage.aspx"); } ProcessMyEventsHere();}#ifdef TESTING_ONLYprotected void SetUpContextActions() { // do stuff here like manipulation of Session store and other // objects in order to create the illusion that I did stuff in the // other (previous) pages of the site before going here}#endif
in that way I can do a #define TESTING_ONLY at the top of the code and the page will execute SetUpContextActions() in order to set up the context that the page expects (like being logged in, having products in the shopping cart, etc.). I can remove the #define once I'm done testing in order for the compiler to skip over those portions with #ifdef ... #endif, so as not to bloat the output assembly/code.
What's your take on this? If you have something elegant and useful it would be well-appreciated Thanks!
Nice question cruizer.
My usual approach is using the Conditional Attribute which is the same as the #if DEBUG directive. Below is a sample code structure:
1234567891011121314151617181920212223242526272829 30
using System; using System.Diagnostics; class Program { static void Main(string[] args) { #if DEBUG DebugMethod(); #endif AnotherDebugMethod(); NonDebugMethod(); Console.ReadLine(); } static void DebugMethod() { //gets called only on debug mode Console.WriteLine("Called DebugMethod"); } [Conditional("DEBUG")] static void AnotherDebugMethod() { //only gets called when in debug mode same as #if DEBUG but a little bit less messy Console.WriteLine("Called AnotherDebugMethod"); } static void NonDebugMethod() { //this gets called all the time Console.WriteLine("Called NonDebugMethod"); } }
↑ Grab this Headline Animator
cruizer:well yes. the reason i brought this up was that my code looks ugly and I wanted to solicit opinion from all of you note though that what I am doing is not TDD nor user interface testing but, in a way, short-circuiting actual testing of my web pages. i wanted to do something like Model-View-Presenter on this one, but MVP is about testing UI logic (the presenter), not getting feedback on the actual look of the pages. what I plan to do to avoid ugly code is to put that code only while I'm developing/testing the page but to remove it completely once I'm done. yun nga lang, sayang naman kung kailanganin ko ulit...
cruizer:but MVP is about testing UI logic (the presenter), not getting feedback on the actual look of the pages.