0

I have this in my controller

public ActionResult Testing()
{
    CustomerContactModel model = new CustomerContactModel();
    ...
    HttpContext.Current.Session["xxxx"] = "Data";

    return PartialView("MyPartialView", model);
}

I get an exception on HttpContext when I get run the controller action from my trest method. How can I solve this problem ?

Thanks,

3 Answers 3

1

Try this:

http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx

Sign up to request clarification or add additional context in comments.

Comments

0

You can moq virtually anything, including the HttpContext, for testing methods:

Try this post as a starter.

Personally I try to make my ActionResult methods not needing testing, by keeping all the important code in the controllers and beyond...but if you do want to test them then mocking can be very handy.

Comments

0

You have to mock HttpContext in your unit tests. Here's how you can do it with Moq framework : How do I mock the HttpContext in ASP.NET MVC using Moq?

But you can also use MvcContrib TestControllerBuilder to do it easily. You have some examples here : http://mvccontrib.codeplex.com/wikipage?title=TestHelper&referringTitle=Documentation

An advice for the future, will be also to avoid as possible to depend on HttpContext in your controllers actions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.