Visual Studio RedirectToAction方法断言的Xunit测试



如何在ASP上为以下代码段编写断言?. NET Core 6.0

If (result != null)

return RedirectToAction("ActionName" ControllerName", routeValues);

这是原问题的重复。你可以写一个测试来检查当前的动作名和控制器名是否被返回当你的代码被调用。

例如-

这是我的控制器-

[HttpGet]
public async Task<IActionResult> Get([FromQuery] string secretName)
{
try
{
return RedirectToAction("TestAction", "WeatherForecast");
}
catch
{
return BadRequest("Error: Unable to read secret");
}
}

这是我的单元测试-

[Fact]
public void Test1()
{
Mock<WebApplication1.Abstract.IKeyValutManager> secretManager = new Mock<IKeyValutManager>();
Mock<Microsoft.Extensions.Logging.ILogger<WeatherForecastController>> logger = new Mock<ILogger<WeatherForecastController>>(MockBehavior.Loose);
WeatherForecastController controller = new WeatherForecastController(logger.Object, secretManager.Object);
var result = controller.Get("test");
Assert.IsType<RedirectToActionResult>(result.Result);
var redirectionResult = (RedirectToActionResult)result.Result;
Assert.True((redirectionResult.ActionName.CompareTo("TestAction")) ==0);
Assert.True((redirectionResult.ControllerName.CompareTo("WeatherForecast")) == 0);
//redirectionResult.RouteValues["controller"].Equals("WeatherForecast");
}

相关内容

  • 没有找到相关文章

最新更新