我使用ASP.NET MVC Core v2.1创建了一个API.我的一个HttpGet方法设置如下:
public async Task<IActionResult> GetConfiguration([FromRoute] int? id)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
..... // Some code here
return Ok(configuration);
}
catch (Exception ex)
{
... // Some code here
}
}
Run Code Online (Sandbox Code Playgroud)
单元测试时,我可以检查Ok是响应,但我真的需要查看配置的值.我似乎无法使用以下内容:
[TestMethod]
public void ConfigurationSearchGetTest()
{
var context = GetContextWithData();
var controller = new ConfigurationSearchController(context);
var items = context.Configurations.Count();
var actionResult = controller.GetConfiguration(12);
Assert.IsTrue(true);
context.Dispose();
}
Run Code Online (Sandbox Code Playgroud)
在运行时,我可以检查是否actionResult有某些我无法编码的值.有什么我做错了吗?或者我只是想到这个错误?我希望能够做到:
Assert.AreEqual(12, actionResult.Values.ConfigurationId);
Run Code Online (Sandbox Code Playgroud)