locked
ASP.net passing QueryString for Unit Testing RRS feed

  • Question

  • Hi,

    Is there any way to pass Query String Parameters to the URL (ASPX Page) thats being Unit Tested? The Query String cannot be part of the UrlToTest attribute as typically these Parameters would be determined at runtime on a previous page before being redirected to the aspx page that would need to be Unit Tested. Any pointers or workaround would be helpful.

    Thanks,
    Krishna

    Thursday, February 16, 2006 4:59 AM

Answers

  • Yes, you are correct. ASP.Net unit tests allow you to run your code in a web server environment (user account, security context, working directory, etc.), but they don't fully simulate the application's environment.

     

    Part of this is intentional in that unit tests are generally meant to test pieces of your code in isolation using wrappers and mocks as you describe. We could/should do more to help you mock the behaviors you list, though. I'll make sure this gets added to our list of features to consider for the next version.

     

    Thanks,

    Tom

    Monday, February 20, 2006 7:30 PM

All replies

  • No, there is no way to dynamically generate the parameters that are passed on the URL for an ASP.Net unit test. Depending on what values you need to send in, you could try one of the following:

     

    - hard-code the inputs in your unit test

    - simulate a call to the previous page as part of your TestInitialize method.

    - use a data-driven test to vary inputs to the unit test

     

    In general, ASP.Net unit tests are intended to test your classes and methods in isolation without their interaction with the rest of the web site. If you are trying to do integration or scenario type testing, you might consider using a recorded web test instead.

    Thursday, February 16, 2006 7:51 AM
  • I was right to worry and expected this to be the reply :-) I was wondering if there was an easier way around to make it work, rather than having to refactor some of my code and access the Parameters to the Page through Wrappers and Mock Objects.

    I assumed the same applies to other aspects and Properties of the Page ? like for instance :-

    1. IsPostBack / IsValid Checks
    2. Response.Redirects
    3. Server Variables / Sessions / Cookies between calls 
    4. Url Referrers
    5. etc.,

    Thanks for the reply though.

    Regards,

    Krishna

    Thursday, February 16, 2006 2:58 PM
  • Yes, you are correct. ASP.Net unit tests allow you to run your code in a web server environment (user account, security context, working directory, etc.), but they don't fully simulate the application's environment.

     

    Part of this is intentional in that unit tests are generally meant to test pieces of your code in isolation using wrappers and mocks as you describe. We could/should do more to help you mock the behaviors you list, though. I'll make sure this gets added to our list of features to consider for the next version.

     

    Thanks,

    Tom

    Monday, February 20, 2006 7:30 PM
  • Hi Krishnaswin,

     

    I'm so glad someone else has run into this problem.  It seems such an obvious problem to me but hardly anyone seems to talk about it.  Maybe I'm missing something.

     

    Did you ever find a solution?  I think I've found one, albeit a bit crude:

     

    Locate the step in your test events where the query string is invoked and drill down to the querystring parameter.  It should say something like OrderID=1234.  In the properties dialog, databind the value property to your database.  Bind the value property to a view in your database which you have previously created.  The view should be something like "SELECT MAX(OrderID) FROM Order".  You'd have to lock other users out while testing, but does this make sense?  I haven't tested it yet, but it looks like it would work.

     

    Andy

    Tuesday, October 17, 2006 5:59 PM
  • I use Typemock Isolator and Ivonna to unit test ASP.NET, in my experience, they are very helpful tools for this.  You don't need a web server to run them, and you can control every scenario you want to test.

    • Proposed as answer by Seamus_m Thursday, April 30, 2009 3:49 PM
    Thursday, April 30, 2009 3:49 PM