Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get TempData in an integration test

I have a controller with the following actions:

[HttpGet]
public IActionResult Index()
{
    return View();
}

[HttpPost]
[Route(
    "/MyShop/OrderDetails/CancelOrder", 
    Name = UrlRouteDefinitions.MyShopOrderDetailsCancelOrder)]
[ValidateAntiForgeryToken]
public IActionResult CancelOrder(MyViewModel viewModel)
{
    var isCancelSuccessful = _orderBusinessLogic.CancelOrderById(viewModel.Order.Id);

    if (isCancelSuccessful)
    {
        //to show a success-message after the redirect
        this.TempData["SuccessCancelOrder"] = true;
    }

    return RedirectToRoute(UrlRouteDefinitions.MyShopOrderDetailsIndex, new
    {
        orderId = viewModel.Order.Id
    });
}

Then I also have the following piece of HTML in the View for the Controller mentioned above:

<div class="panel-body">
    @if (TempData["SuccessCancelOrder"] != null) 
    {
        //show the message
        @TempData["SuccessCancelOrder"].ToString();
    }
</div>

Now I'm writing an integration test (code-summary below) to check if the CancelOrder() method works as expected. There I'd like to access the value of the TempData dictionary to check its correctness.

[TestMethod]
public void MyTest()
{
    try
    {
        //Arrange: create an Order with some Products
        var orderId = CreateFakeOrder();

        //Open the Order Details page for the arranged Order
        var httpRequestMessage = base.PrepareRequest(
            HttpMethod.Get,
            "/MyShop/OrderDetails?orderId=" + orderId,
            MediaTypeEnum.Html);

        var httpResponse    = base.Proxy.SendAsync(httpRequestMessage).Result;
        var responseContent = httpResponse.Content.ReadAsStringAsync().Result;
        var viewModel       = base.GetModel<MyViewModel>(responseContent);

        Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode);


        //Try to execute a POST to cancel the Order
        httpRequestMessage = base.PrepareRequest(
            HttpMethod.Post,
            "/MyShop/OrderDetails/CancelOrder",
            MediaTypeEnum.Html,
            httpResponse, content: viewModel);


        httpResponse = base.Proxy.SendAsync(httpRequestMessage).Result;
        var expectedRedirectLocation = $"{this.RelativeHomeUrl}/MyShop/OrderDetails?orderId=" + orderId;
        var receivedRedirectLocation = WebUtility.UrlDecode(httpResponse.Headers.Location.ToString());


        //we expect that the Order Details page will be reloaded 
        //with the ID of the already cancelled Order
        Assert.AreEqual(HttpStatusCode.Redirect, httpResponse.StatusCode);

        //-----------------------------------------------------------
        //here I'm going to re-execute a GET-request 
        //on the Order Details page. 
        //
        //Then I need to check the content of the message 
        //that's been written in the TempData
        //-----------------------------------------------------------
    }
    finally
    {
        //delete the arranged data
    }
}

In any case, I don't know how to access it from my integration test. Does anybody know how to do it and if there's a way at all?

like image 573
dario Avatar asked Jul 26 '17 14:07

dario


1 Answers

Controller.TempData is public so you can easily access it and check if your key/value exists

[TestMethod]
public void TempData_Should_Contain_Message() {    
    // Arrange
    var httpContext = new DefaultHttpContext();
    var tempData = new TempDataDictionary(httpContext, Mock.Of<ITempDataProvider>());
    var controller = new TestController();
    controller.TempData = tempData;

    // Act
    var result = controller.DoSomething();

    //Assert
    controller.TempData["Message"]
        .Should().NotBeNull()
        .And.BeEquivalentTo("Hello World");

}
like image 121
Nkosi Avatar answered Oct 28 '22 15:10

Nkosi