Global.asax.cs and unittests

Discussion in 'ASP .Net' started by Peter Larsen [CPH], Sep 5, 2008.

  1. Hi,

    I have a problem where the code in Global.asax.cs runs before the unittests
    (on test of course).

    In the global file, i load some data from a Oracle table and save it in
    HttpApplicationState. This is happening in the Application_start function.

    The following unittest show the beginning of the method, where the database
    mock is created. If this mock isn't created, the unittest will try to access
    the database - which is not allowed/possible from the build server.

    [TestMethod()]
    [HostType("ASP.NET")]
    [AspNetDevelopmentServerHost("C:\...Web", "/")]
    [UrlToTest("http://localhost:2044/")]
    [DeploymentItem("...Web.dll")]
    public void PageTest()
    {
    DatabaseAccess.CreateDataAccessMock();
    //the test code

    }

    The problem is that i want the mock to be created before the Global.asax.cs
    file is called.
    Is that possible ??

    Thank you in advance.

    BR
    Peter
    Peter Larsen [CPH], Sep 5, 2008
    #1
    1. Advertising

  2. Hi Peter,

    Regarding on this issue, you 're using unit testing framework to perform
    test on your ASP.NET web page, however, you found some code in Global.asax
    always get executed first before some of your unit test intilize code, you
    want to make the unit test code executed first, correct?

    Based on my understanding, ASP.NET application is a particulur one as
    ASP.NET appdomain is hosted by an existing process. And at the startup
    time, the code in global.asax is always run first. I'm afraid so far there
    hasn't provided inteface for customize the execution time or sequence of
    the global.asax(it is executed before each page request get executed, I
    think the unit test code is executing together with each page request).

    My current thought is whether it is convenient to move those initialize
    code(for unit test) into global.asax? thus, all the unit test functions
    only contains code that is ok to execute after the global.asax

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 1 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions or complex
    project analysis and dump analysis issues. Issues of this nature are best
    handled working with a dedicated Microsoft Support Engineer by contacting
    Microsoft Customer Support Services (CSS) at
    http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.

    --------------------
    >From: "Peter Larsen [CPH]" <>
    >Subject: Global.asax.cs and unittests
    >Date: Fri, 5 Sep 2008 20:57:30 +0200


    >Hi,
    >
    >I have a problem where the code in Global.asax.cs runs before the

    unittests
    >(on test of course).
    >
    >In the global file, i load some data from a Oracle table and save it in
    >HttpApplicationState. This is happening in the Application_start function.
    >
    >The following unittest show the beginning of the method, where the

    database
    >mock is created. If this mock isn't created, the unittest will try to

    access
    >the database - which is not allowed/possible from the build server.
    >
    > [TestMethod()]
    > [HostType("ASP.NET")]
    > [AspNetDevelopmentServerHost("C:\...Web", "/")]
    > [UrlToTest("http://localhost:2044/")]
    > [DeploymentItem("...Web.dll")]
    > public void PageTest()
    > {
    > DatabaseAccess.CreateDataAccessMock();
    > //the test code
    >
    > }
    >
    >The problem is that i want the mock to be created before the

    Global.asax.cs
    >file is called.
    >Is that possible ??
    >
    >Thank you in advance.
    >
    >BR
    >Peter
    >
    >
    >
    Steven Cheng [MSFT], Sep 8, 2008
    #2
    1. Advertising

  3. Hi Steven,

    Thanks for your reply.

    I use unittests to test my business logic - and yes, i have found that
    Global.asax always get fired first.
    This is also what you say. And you also say that there is no way around
    this.

    The code in Global.asax has no importans for the unittest.
    If it was possible, i would disable Global.asax when running unittests.

    The only solution i can think of, is to move the code from global.asax and
    to the default page - or to all pages and then ensure the code is only run
    once. Only that way, the unittests run before any business logic.

    BR
    Peter
    Peter Larsen [CPH], Sep 9, 2008
    #3
  4. Thanks for your reply Peter,

    I think your workaround it also reasonable. You can remove the global.asax
    and put the code logic to a default page which is guranteed to be run at
    the earlier time of your web application.

    Sincerely,

    Steven Cheng

    Microsoft MSDN Online Support Lead


    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.




    --------------------
    >From: "Peter Larsen [CPH]" <>
    >References: <#>

    <>
    >Subject: Re: Global.asax.cs and unittests
    >Date: Tue, 9 Sep 2008 10:15:54 +0200


    >
    >Hi Steven,
    >
    >Thanks for your reply.
    >
    >I use unittests to test my business logic - and yes, i have found that
    >Global.asax always get fired first.
    >This is also what you say. And you also say that there is no way around
    >this.
    >
    >The code in Global.asax has no importans for the unittest.
    >If it was possible, i would disable Global.asax when running unittests.
    >
    >The only solution i can think of, is to move the code from global.asax and
    >to the default page - or to all pages and then ensure the code is only run
    >once. Only that way, the unittests run before any business logic.
    >
    >BR
    >Peter
    >
    >
    >
    Steven Cheng [MSFT], Sep 10, 2008
    #4
    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. =?Utf-8?B?YmNoYXJsZXM=?=

    global.asax and global.asax.cs

    =?Utf-8?B?YmNoYXJsZXM=?=, Oct 4, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    825
    =?Utf-8?B?YmNoYXJsZXM=?=
    Oct 5, 2004
  2. Mark Rae

    Global.asax / Global.asax.cs in v2

    Mark Rae, May 23, 2006, in forum: ASP .Net
    Replies:
    6
    Views:
    3,152
    Mark Rae
    May 23, 2006
  3. Daniel

    Python and database unittests

    Daniel, Aug 26, 2008, in forum: Python
    Replies:
    9
    Views:
    765
    M.-A. Lemburg
    Aug 27, 2008
  4. Cameron Simpson
    Replies:
    3
    Views:
    272
    Terry Reedy
    Apr 23, 2010
  5. Ulrich Eckhardt

    unittests with different parameters

    Ulrich Eckhardt, Nov 22, 2010, in forum: Python
    Replies:
    9
    Views:
    285
    Ulrich Eckhardt
    Nov 24, 2010
Loading...

Share This Page