小编Chr*_*lds的帖子

如何从通过API进行单元测试返回的Task <IActionResult>中获取值

我使用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)

c# asp.net-core-mvc .net-core

11
推荐指数
4
解决办法
1万
查看次数

标签 统计

.net-core ×1

asp.net-core-mvc ×1

c# ×1