NUnit and HttpContext

Discussion in 'ASP .Net' started by JahMic, Jan 31, 2007.

  1. JahMic

    JahMic Guest

    I recently took on the unenviable task of using the HttpContext object
    in my unit tests, as any body who has tried this before knows it is
    not so easy. The three main approaches I have found are: 1) Making a
    mock object, 2) Faking a Context via SimpleWorkerRequest, and 3)
    Creating your own host environment ala CreateApplicationHost.

    I didn't like the first approach, because their might be some
    variation between a mock object and the real thing. That reduces a lot
    of the power of unit testing. (...thoughts?) The Second approach is
    fairly well publicized over the web but seems to only good for .NET
    1.x. Trying some of the projects unchanged in a 2.0 environment
    simply does not work, as the WorkerRequest seems to want to work out
    of an actual host environment.

    The third approach is promising but I'm still having some problems
    getting the HttpContext into the unit tests. A good article on this
    approach:
    http://hyperthink.net/blog/CommentView,guid,271632d2-07e3-41af-9e58-9a7e25348b8c.aspx
    In it, he exposes the HttpContext by a call to:
    System.Runtime.Remoting.Messaging.CallContext.SetData("HttpContext",
    request).
    Pretty cool, except when the execution returns back at the unit test
    call, HttpContext.Current is still null. Thoughts?

    To either return the HttpContext in a call or expose it as a public
    field causes NUnit to throw an exception that it is not
    serializable... Serializable??? I don't particularly if it's
    serialized or not... Marking the class as [Serializable] does not
    help and I'm now at a loss on how to get through this. Thoughts?

    Any help, much appreciated.

    J
     
    JahMic, Jan 31, 2007
    #1
    1. Advertising

  2. On Jan 31, 6:38 am, "JahMic" <> wrote:
    > I recently took on the unenviable task of using the HttpContext object

    [snip]

    There's very few who think of this, but in fact a Singleton class will
    in a Web Application basically work as the Application object in the
    matters that it'll "never die"...
    If you need to have objects that exists on the cross of http calls
    (statefulness like Session, HttpContext and Application) you CAN in
    fact use a Singleton class since that "one instance object" will NOT
    be destroyed and recreated before the application is killed...
    :)

    ..t

    --
    http://ajaxwidgets.com
    Free ASP.NET Ajax Widgets NOW!
     
    Thomas Hansen, Jan 31, 2007
    #2
    1. Advertising

  3. JahMic

    JahMic Guest

    On Jan 31, 5:34 pm, "Thomas Hansen" <> wrote:
    > On Jan 31, 6:38 am, "JahMic" <> wrote:> I recently took on the unenviable task of using the HttpContext object
    >
    > [snip]
    >
    > There's very few who think of this, but in fact a Singleton class will
    > in a Web Application basically work as the Application object in the
    > matters that it'll "never die"...
    > If you need to have objects that exists on the cross of http calls
    > (statefulness like Session, HttpContext and Application) you CAN in
    > fact use a Singleton class since that "one instance object" will NOT
    > be destroyed and recreated before the application is killed...
    > :)
    >
    > .t
    >
    > --http://ajaxwidgets.com
    > Free ASP.NET Ajax Widgets NOW!


    Hi Thomas,

    Sounds promising but I tried to think of several ways a Singleton
    class could help and nothing seemed to pan out. Could you elaborate
    on your thoughts a bit more?

    Also, it seems the problem with CreateApplicationHost approach is that
    it creates a seperate domain space where remoting gets involved.
     
    JahMic, Feb 1, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave

    NUnit

    Dave, Aug 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    551
  2. Nick Zdunic
    Replies:
    0
    Views:
    953
    Nick Zdunic
    Nov 5, 2003
  3. Mark

    NUnit and Session state

    Mark, Mar 2, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    3,330
  4. Rippo
    Replies:
    0
    Views:
    338
    Rippo
    Dec 2, 2005
  5. Tony

    Dim context As HttpContext = HttpContext.Current

    Tony, Mar 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    349
    Teemu Keiski
    Mar 3, 2004
Loading...

Share This Page