Home  /  Questions  /  Question



0   0
Jan 10, 2016


Fake HttpContext not needed

Hi, thank you for this great product! Here comes my first question:
  • Product: .Net Design Pattern Framework 4.5 c# Edition
  • MVC unitttest
  • MoqHelpers

In the mvc unit test project there is a sample shopcontrollertest, and the test method SearchTest is using a Fake HttpContext setup. I wonder what is the use for the fake Httpcontext? It doesnt really do anything since you can comment away the  FakeHttpContext setup and the test will still pass.

br/ John



// test Product Search Action method. Returns sorted product list.
// Note: this creates a Fake HttpContext which is necessary to access an [HttpPost] decorated action method.
[TestMethod]
       public void SearchTest()
       {
           // Arrange
           var controller = CreateShopController();
 
           controller.SetFakeControllerContext();
           controller.Request.SetHttpMethodResult("POST");
 
           // Act
           var result = controller.Search() as ViewResult;
 
           // Assert
           Assert.IsInstanceOfType(result, typeof(ViewResult));
           Assert.IsInstanceOfType(result.ViewData.Model, typeof(SearchModel));
 
           // check products returned
 
           var model = result.ViewData.Model as SearchModel;
           Assert.AreEqual(1, model.Products.List.Count());
       }