System.Exception,xUnit 在控制器上调用 HttpDelete 操作
当我调用:
var result = controllerApi.DeleteCliente(2);
DeleteCliente 代码(在控制器内):
[Authorize(Roles = "PaginaDeClientes")]
[HttpDelete]
public IActionResult DeleteCliente(int clienteId)
{
var cliente = _context.Clientes.Find(clienteId);
if (cliente == null) { return NotFound(); }
_context.Clientes.Remove(cliente);
var result = _context.SaveChanges();
if (result <= 0)
{
throw new Exception();
}
return Ok();
}
时,我收到“System.Exception:引发了类型为“System.Exception”的异常”。” 我的测试DeleteCliente():
[Fact]
public void DeleteCliente()
{
var controllerApi = new PoollGest.Controllers.Api.ClientesController(_context);
_context.Clientes.Add(new Cliente()
{
ClienteId = 2,
Nome = "Jose",
Desconto = 20,
});
var result = controllerApi.DeleteCliente(2);
Assert.IsType<OkResult>(result);
}
这个控制器当前只有deleteCliente操作,作为参考,我运行了其他测试,在我的上下文中创建一个对象并运行“crud”操作之一,但我没有问题,不确定是什么我在这里做错了
I'm getting "System.Exception : Exception of type 'System.Exception' was thrown." when I'm calling:
var result = controllerApi.DeleteCliente(2);
the DeleteCliente code (inside the controller):
[Authorize(Roles = "PaginaDeClientes")]
[HttpDelete]
public IActionResult DeleteCliente(int clienteId)
{
var cliente = _context.Clientes.Find(clienteId);
if (cliente == null) { return NotFound(); }
_context.Clientes.Remove(cliente);
var result = _context.SaveChanges();
if (result <= 0)
{
throw new Exception();
}
return Ok();
}
My test DeleteCliente():
[Fact]
public void DeleteCliente()
{
var controllerApi = new PoollGest.Controllers.Api.ClientesController(_context);
_context.Clientes.Add(new Cliente()
{
ClienteId = 2,
Nome = "Jose",
Desconto = 20,
});
var result = controllerApi.DeleteCliente(2);
Assert.IsType<OkResult>(result);
}
This controller currently only has the deleteCliente action, for reference I have run other tests where I create an object in my context and run one of the "crud" action's and I had no problems, not sure what I'm doing wrong here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我使用断点运行调试时,我找不到问题的确切原因,但现在可以了。
我基本上重建了整个项目,删除了迁移,再次构建了bd。现在它可以工作了,所以如果你有类似的问题,我建议你重建你的项目。
I couldn't find the exact cause of the problem when I run the debug with the breakpoints, but it works now.
I Basically rebuild the entire project, deleted the migrations, build the bd again. And it works now, so if you have a similar problem I recommend you to rebuild your project.