I try to make a test with xUnit finding the Id in a query in the controller.
My code its.
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.);
}
}
This is my method
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));
}
I don't know how can validate if the result contain the Id with the result.
Please help.
If you cast the result, you can pull out the value:
var value = (result as OkObjectResult).Value;
Assert.Contains(1, value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With