【问题标题】:Make a test in xUnit with Assert.Contains使用 Assert.Contains 在 xUnit 中进行测试
【发布时间】:2019-11-22 23:36:39
【问题描述】:

我尝试使用 xUnit 在控制器的查询中查找 Id 进行测试。

我的代码。

 public class CuestionarioTest
{
public readonly CuestionarioController _controller;
private readonly Mock<ICuestionarioServicio> _cuestionariosServicio;
private readonly Mock<IRespuestaServicio> _respuestaServicio;

public CuestionarioTest()
{
    _cuestionariosServicio = new Mock<ICuestionarioServicio>();
    _respuestaServicio = new Mock<IRespuestaServicio>();
    _controller = new  CuestionarioController(_cuestionariosServicio.Object, _respuestaServicio.Object);
}    
[Fact]
public async Task ComprobarBusquedaPorId()
{
    int id = 1;
    var result = await _controller.BuscarPorId(id);
    //Assert.IsType<OkObjectResult>(result);
    Assert.Contains(1, result.);    
}

}

这是我的方法

public class CuestionarioController : Controller
{
    private readonly ICuestionarioServicio _cuestionariosServicio;
    private readonly IRespuestaServicio _respuestaServicio;

    public CuestionarioController(ICuestionarioServicio cuestionarioServicio, IRespuestaServicio respuestaServicio)
    {
        _cuestionariosServicio = cuestionarioServicio;
        _respuestaServicio = respuestaServicio;
    }
    public async Task<IActionResult> BuscarPorId(int id)
    {
        return Ok(await _cuestionariosServicio.ObtenerPorId(id));
    }

我不知道如何验证结果是否包含带有结果的 Id。

请帮忙。

【问题讨论】:

标签: c# xunit


【解决方案1】:

如果你投出结果,你可以拉出值:

var value = (result as OkObjectResult).Value;
Assert.Contains(1, value);

【讨论】:

  • 我有一个错误,对象可以更改为 System.Collections.Generic.IEnumerable
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-02
  • 2011-03-05
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
相关资源
最近更新 更多