red*_*x17 5 c# unit-testing .net-core asp.net-core
我在控制器中有异步方法,可以从客户端调用该方法。我想在 foreach 循环中迭代此方法,但出现错误:
*Cannot convert from 'Microsoft.AspNetCore.Mvc.ActionResult' to 'System.Collections.Generic.IEnumerable<string>'*
Run Code Online (Sandbox Code Playgroud)
单元测试.cs
[TestFixture]
public class ClientUnitTests
{
private MessagesController Controller = new MessagesController(null, null, null);
[Test]
public async Task Check_API_Links()
{
// Arrange
List<IEnumerable<string>> reporteeList = new List<IEnumerable<string>>();
var links = await Controller.GetLinks();
reporteeList.Add(links); //Cannot convert from 'Microsoft.AspNetCore.Mvc.ActionResult' to 'System.Collections.Generic.IEnumerable<string>'
foreach (var itemLink in reporteeList)
{
reporteeList.Add(itemLink);
}
// Assert
Assert.IsNotNull(reporteeList);
Assert.GreaterOrEqual(0, reporteeList.Count);
Assert.IsInstanceOf<IEnumerable<string>>(reporteeList);
}
}
Run Code Online (Sandbox Code Playgroud)
控制器.cs
[HttpGet]
public async Task<ActionResult> GetLinks()
{
var result = await _client.GetLinks();
return Ok(result);
}
Run Code Online (Sandbox Code Playgroud)
客户端.cs
public async Task<ActionResult<IEnumerable<string>>> GetLinks()
{
// Some logic here,
}
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题以在单元测试中迭代 GetLinks 方法?
谢谢。
假设.NET Core 2.1
public async Task<ActionResult<IEnumerable<string>>> GetLinks()
Run Code Online (Sandbox Code Playgroud)
或较不优选的
public async Task<IActionResult> GetLinks()
Run Code Online (Sandbox Code Playgroud)