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

  • Home
  • SEARCH
  • 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 8105779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:15:28+00:00 2026-06-06T00:15:28+00:00

Im trying to write some unit tests for a couple of merthods that are

  • 0

Im trying to write some unit tests for a couple of merthods that are dependant on the HttpContext object. I have come up with the following class but I dont think its the best way to do it.

internal class HttpContextMocking
{

    public static HttpContext Context()
    {

        HttpContext context = new HttpContext(new SimpleWorkerRequest("", "", "", null, new StringWriter()));

        context.Cache.Insert("SomeName", "value", null, DateTime.Now.AddMinutes(3), TimeSpan.Zero);

        return context;
    }

I then call this mock HttpContext in my unit test in the following way:

    [TestMethod]
    [TestCategory("Web")]
    public void Get()
    {
        HttpContext.Current = HttpContextMocking.Context();

        object result = CacheUtility.Get("NonExistentItem");

        Assert.IsNull(result);
    }

is there a better way of implementing this, something that would be cleaner when I start to add more dummy data.

  • 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-06-06T00:15:29+00:00Added an answer on June 6, 2026 at 12:15 am

    HttpContext is really complex to mock. I will suggest to change the dependency to an interface that provides what you need, and then have two implementations: a real one, using HttpContext and a Mock one for the tests.

    If you anyway want to mock it and your code does the trick, leave it that way.

    Another option is to use the ASP.NET host type for MSTest. In this case, you will be performing an actual request and your HttpContext will be there:

    [TestMethod]
    [HostType("ASP.NET")]
    [AspNetDevelopmentServerHost(@"$(SolutionDir)\Tests", "/")]
    [UrlToTest("http://localhost:1234/TestPage.aspx")]
    public void MyTest()
    {
        Page page = testContextInstance.RequestedPage;
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write some unit tests for a JPA model that I've built
I am trying to write some Unit Tests to test some custom NSOperations that
I'm trying to write some unit tests for Command object validations. When my command
I'm trying to find a way to write some unit-tests that can be used
I am trying to write some unit tests that test the endpoints for my
I have some Web API methods that I want to write unit tests for.
I am trying to write some unit tests and I was wondering if there
I am trying to write some unit tests for a small web service written
I'm attempting to write some unit tests using EasyMock and TestNG and have run
I am trying to write some unit tests for my MVC3 project (the first

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.