gpt4 book ai didi

c# - 如何在集成测试中获取 TempData

转载 作者:太空狗 更新时间:2023-10-30 00:37:45 26 4
gpt4 key购买 nike

我有一个具有以下操作的 Controller :

[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
});
}

然后我在上面提到的 Controller 的 View 中还有以下 HTML:

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

现在我正在编写一个集成测试(下面的代码摘要)来检查 CancelOrder() 方法是否按预期工作。我想访问 TempData 字典的值来检查其正确性。

[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
}
}

无论如何,我不知道如何从我的集成测试中访问它。有人知道怎么做吗?如果有办法的话?

最佳答案

Controller.TempData 是公开的,因此您可以轻松访问它并检查您的键/值是否存在

[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");

}

关于c# - 如何在集成测试中获取 TempData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45329165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com