Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 992741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:18:54+00:00 2026-05-16T06:18:54+00:00

I want to write a unit test which tests the function of a class

  • 0

I want to write a unit test which tests the function of a class called UploadedFile.

The problem I face is this class’ static constructor uses HttpContext.Current property and because I am running my unit test from a class library I do not have an HttpContext at the testing time.

Look at my static constructor:

static UploadedFile()
{
    if (HttpContext.Current == null)
        throw new Exception("web server not available");

    HttpServerUtility server = HttpContext.Current.Server;

    // SET UploadedFileMappingFile Names:
    _resourceFileNames = new StringDictionary();

    _resourceFileNames[_suppoertedFileStructures] = server.MapPath(SupportedUploadedFileStructures);
    _resourceFileNames[_supportedFileStructuresXSD] = server.MapPath(SupportedUploadedFileStructuresXSD);

    _resourceFileNames[UploadedFileEnum.UploadedFileFormatENUM.CSV.ToString()] = server.MapPath(UploadedFileColumnMap);        
}

What should I do in my testing environment so that HttpContext.Current won’t be null and I can successfully set this:

 HttpServerUtility server = HttpContext.Current.Server;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:18:55+00:00Added an answer on May 16, 2026 at 6:18 am

    You shouldn’t use HttpContext.Current directly in your function as it is close to impossible to unit test, as you’ve already found out. I would suggest you using HttpContextBase instead, which is passed either in the constructor of your class or as an argument to the method you are testing. This will allow the consumers of this class to pass a real HttpContextWrapper and in your unit test you can mock the methods you need.

    For example here’s how you could call the method:

    var wrapper = new HttpContextWrapper(HttpContext.Current);
    Foo.UploadedFile(wrapper);
    

    And in your unit test (using Rhino Mocks):

    var contextMock = MockRepository.GenerateMock<HttpContextBase>();
    // TODO: Define expectations on the mocked object
    Foo.UploadedFile(contextMock);
    

    Or, if you prefer, use Constructor Injection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 538k
  • Answers 538k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Once you've checked out the source code, you should be… May 17, 2026 at 2:03 am
  • Editorial Team
    Editorial Team added an answer Left(drv.Row("booking_status"), 10) returns a String. There is no overload for… May 17, 2026 at 2:03 am
  • Editorial Team
    Editorial Team added an answer Jon Skeet's / Timwi's solutions are the way to go.… May 17, 2026 at 2:03 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I want to create a unit test for a member function of a class
I need to write a unit test for a function that returns a dictionary.
I'm trying to write a unit test for my FileWatcher class. FileWatcher derives from
Hy, I'm writing a class which should help me in unit tests. The class
I have a bunch of repository classes which I want to unit-test using Visual
I have a a class called ViewFactory and this class should deliver the right
I am trying to write a unit test for a method that uses a
I'm trying to write a unit test with phpunit for a model that uses
I am trying to write a unit test using .NET 4 to ensure that
Two questions about unit tests. I've been writing unit tests for a while, however

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.