Global.asax.cs and unittests

  • Thread starter Thread starter Peter Larsen [CPH]
  • Start date Start date
P

Peter Larsen [CPH]

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
 
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:
(e-mail address removed).

==================================================
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]" <[email protected]>
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
 
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
 
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:
(e-mail address removed).

==================================================
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]" <[email protected]>
References: <#[email protected]>
 
Back
Top