.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal
Post New Web Links

How do I unit test HttpContext

Posted By:      Posted Date: October 21, 2010    Points: 0   Category :ASP.Net


I have a class library that uses HttpContext a lot, but I am having trouble writing unit tests for the functions. e.g. System.Web.HttpContext.Current.Request.ApplicationPath always return null, that makes class library throws exceptions everywhere.  I am not allowed to change the source codes of class library, so please let me know how to write test cases against it.


View Complete Post

More Related Resource Links

Generation Test: Automated Unit Tests for Legacy Code with Pex


Creating and maintaining a unit test suite for legacy code can be a challenge. Pex automatically produces a small test suite with high code and assertion coverage.

Nikhil Sachdeva

MSDN Magazine December 2009

Unit Testing: Apply Test-Driven Development to your Database Projects


Jamie Laflen extols the benefits of TDD when applied to database development-and supplies some useful techniques along the way.

Jamie Laflen

MSDN Magazine Launch 2008

Unit Testing: Exploring The Continuum Of Test Doubles


Creating and using mock component servers simplifies unit testing. Use these examples to get started.

Mark Seemann

MSDN Magazine September 2007

Unit Testing: Mock Objects to the Rescue! Test Your .NET Code with NMock


The problem: unit testing libraries, especially data access components, that have a complex set of dependencies. The solution: providing a dynamic mock implementation of your data access classes. Here the author explains just how to make testing easier and more reliable using NMock.

Mark Seemann

MSDN Magazine October 2004

Who know some tools can help to create fake data which was used in unit test of asp.net mvc project

I was using aps.net mvc unit test, and want to create fake data instead of using real database connection, but the Linq query code and real database was huge and complicated, create fake data in unit test which can mimic real database was a huge job! Who knows some tools can help me to decrease size of real database and create mimic fake data for unit test. Thanks

Unit Test in MVC. What frameworks do you suggest?

Hello,What Unit Test Framework and Mock would you advice me for test MVC projects.Thank you,Miguel

How to unit test C# Web Service with Visual Studio 2010 ultimate


I have webservice running, how to test those web methods using the Test features of Visual Studio 2010.

As with normal web reference i am not able to create the classes and call the webmethods inside the test class.

Write Unit Test for method with parameter


I'm trying to build my first MVC site and also write unit test for the first time.  I can't figure how to write unit test for methods that take parameters.  How do I write a test method for this controller action?

public class CustomerController : Controller

        public ActionResult Details(int id)
            var customerDetails = (from c in _entities.Customers
                                  where c.PK == id
                                  select c).FirstOrDefault();

            return View(customerDetails);


Unit Testing Error - The unit test adapter failed to connect to the data source or to read the data


I'm using VSTS 2K8 and I've set up a Unit Test Project. In it, I have a test class with a method that does a simple assertion. I'm using an Excel 2007 spreadsheet as my data source.

My test method looks like this:

"Dsn=Excel Files;dbq=|DataDirectory|\\MyTestData.xlsx;defaultdir=C:\\TestData;driverid=1046;maxbuffersize=2048;pagetimeout=5",
public void State_Value_Is_Set()

Visual Studio Unit Test for Visual Web Developer 2010 Express



I am using ASP.NET MVC and want to have Unit Test functionality integrated within Visual Web Developer 2010 Express (or Visual Studio LightSwitch). Is it possible and if so, what is the recommended/best way to get it?


Unit Test Url.Action




I have this method in a controller, but when i call from Unit test it is always null, If i run the website it works just fine.  How can i fake it so that Url.Action works
within my controller????? 

/// <summary>
        /// Set landing page URL
        /// </summary>
        /// <param name="contentSubType">Content sub type</param>
        /// <returns>Returns the landing page url</returns>
        public string GetLandingPageByContentSubType(ContentTypeLookup contentSubType)
            var landingPage = string.Empty;

            switch (contentSubType)
                case ContentTypeLookup.DesignArticle:
                    landingPage = SiteGlobalSettings.WebsiteUrl;
                    landingPage += Url.Action("DesignHome", "Design");
                case ContentTypeLookup.Product:
                    landingPage = SiteGlobalSettings.WebsiteUrl;
                    landingPage += Url.Action("ProductsHome", "Products");
                case Co

Why MVC best for UNIT test and not webforms if there is BL and Input Validation Layer?



ASP.NET MVC sessions and books say ASP.NET MVC enables to do unit testing as logic would be in controller independent to HTTP Request. So that controller can be unit tested indivisually. I do not think so people write their bussiness logic in controllers rather in seperate bussiness layer assembly. So there is no need to test controllers in bussiness perspective.

I do not think so we write web applications without a seperate Bussiness Layer. In this case Bussiness Layer also a clear seperation from UI. So BL can also be tested very well using UNIT testing frameworks either in MVC or in webforms.

then why should we chose ASP.NET MVC over WebForms in terms of UNIT Testing. Could anyone please clarify me in this regard if I'm wrong?

Deploying SQL CE dll's for VS2010 Unit Test


I'm trying to uint test the methods of a class which derives from a base class in another assembly. The base class uses Entity with SQL CE.

Normally the System.Data.SqlServerCe.dll and System.Data.SqlServerCe.Entity.dll are in the common build target directory, along with the base class assembly's dll. They don't get copied to the TestResults/<test>/Out directory. (The test assemblies do NOT target the comon build target directory; they use "bin/Release" or "bin/Debug".)

The base class assembly dll DOES get copied to the Out directory, along with other project dll's it depends on. These also have been copied to the bin/Debug (bin/Release) for the unit test project, but the SqlServerCe dll's are not there, either.

I tried using [DeploymentItem("System.Data.SqlServerCe.dll")] to copy them to the Out directory, but with no luck.

Adding the files to the Deployment list in the Test Settings, DOES copy them to Out. However, the unit test still fails in the same way... An exception is being thrown when the main entity class (derived from ObjectContext) is constructed with the connectionString.  The exception message is:

The specified store provider cannot be found in the configuration, or is not vali

Unit Testing SharePoint - Past, Present, and Sporm

As I described in SharePoint: The Wild West of Software Development there is a serious problem when you develop for SharePoint: ensuring quality through unit testing is really, really hard. And that's where a new open source tool just released today called sporm (SharePoint Object Relational Mapper) comes in. While sporm provides many benefits besides simplified unit testing I wanted to focus on this topic first, because sporm's approach, which models the entity framework in the way it supports POCO's, is a unique feature not available with other SharePoint tools like LINQ to SharePoint.

HttpContext.Items and Server.Transfer/Execute

The problem was basically that when a Server.Transfer call was applied the script container (and also various ClientScriptProxy script embedding routines) would potentially fail to load up the specified scripts.

Test your skill in SQL Server

You will be given a series of 10 SQL Server related questions that are randomly selected. Since the questions are randomly selected, you will receive a different set of questions each time you start a test. At the end of the last question, you will be given your score. You can then start a new test session if you like.

Users getting logged out because HttpContext.Current is null


We've  been trying to fix this one for a couple years, and I'm just out of ideas, so any help is really appreciated!

We've got a fairly large, complex web application that uses Forms Authentication to authenticate users.  Throughout the application we store and retrieve information about users in the Session object, and in some cases in cookies.  In a couple places we check for the existence of the context, and if it's null we send the user back to the login page to re-establish the session. 

Just a quick run-down of this web app: C#, .NET 3.5, IIS 6, ASP.NET State Service to manage session

As for the session timer, we use our own home-grown timer, which is basically a client-side timer, which is backed up by a check to the SQL database to see when their last activity was.  This seems to work well for us.  It's not perfect, but it allows us to notify the user before the session times out, and allows us to be certain we're not logging a user out before their 60 minutes of inactivity is up.

What's happening is that certain users are being logged out after just a few minutes.  We've eliminated the timer as a cause and believe what's happening is the HttpContext.Current is null, so the user is logged out.  We do not know why the HttpContext.Current is null, and I underst

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend