System.Exception,xUnit 在控制器上调用 HttpDelete 操作

发布于 2025-01-13 07:40:56 字数 1457 浏览 2 评论 0原文

当我调用:

  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

Error:
error

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み格子的夏天 2025-01-20 07:40:56

当我使用断点运行调试时,我找不到问题的确切原因,但现在可以了。
我基本上重建了整个项目,删除了迁移,再次构建了bd。现在它可以工作了,所以如果你有类似的问题,我建议你重建你的项目。

fix

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.

fix

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文