AkmecNurik
AkmecNurik

Reputation: 205

Mocking HttpContext (Session)


I've read many articles and blogs about mocking in mvc... Many of them were helpful, but i still have some problems:

Upvotes: 1

Views: 806

Answers (1)

Jonas Høgh
Jonas Høgh

Reputation: 10874

You can use tools such as the MVC-contrib TestHelper

This sample from the site shows how to test an action that stores a posted form value in the session

[Test]
public void AddSessionStarShouldSaveFormToSession()
{
    TestControllerBuilder builder = new TestControllerBuilder();
    StarsController controller = new StarsController();
    builder.InitializeController(controller);

    //note that this is assigned before the controller action. This simulates the server  filling out the form data from the request
    builder.Form["NewStarName"] = "alpha c";

    //this assumes that AddSessionStar takes the form data and adds it to the session
    controller.AddSessionStar();

    Assert.AreEqual("alpha c", controller.HttpContext.Session["NewStarName"]);
}

Upvotes: 2

Related Questions