从Xunit中的执行值中获取状态代码

发布于 2025-02-01 20:12:48 字数 1677 浏览 2 评论 0原文

我想比较控制器执行中的返回值,无论是否满足条件。通过调试,我注意到“状态代码”,但没有一种方法来捕获该值进行比较。我的测试代码是

    public void PostAmount_Withdraw_Returns_Null()
    {
        // Arrange
        Amount amount = new Amount() { Id = 0, PaymentDate = DateTime.Today, 
             PaymentTypeId = 3, PaymentAmount = 500, ClientId = 1, Balance = 0 };

        _accountServiceMock.Setup(s => s.Withdraw(amount)).Throws(new Exception());
        
        var target = SetupAmountsController();

        //Act
        var actual = target.PostAmount(amount);

        //Assert

        actual.ShouldNotBeNull();
        //actual.Value.ShouldBe(response);
    }

以下调用控制器方法: -

    public ActionResult<Amount> PostAmount(Amount amount)
    {
        try
        {
            if (amount.PaymentTypeId == 1)
            {
                _accountService.Deposit(amount);
            }
            else if (amount.PaymentTypeId == 2)
            {
                _accountService.Withdraw(amount);
            }
            else
            {
                return Problem("Model 'Amounts'  is null.");
            }
            return new OkObjectResult(new { Message = "Payment Success!" });
        }
        catch (DbUpdateConcurrencyException)
        {
            return Problem("There was error for updating model Payment");
        }
    }

,然后在单位测试执行后返回值

我想在下面提供条件喜欢: -

 actual.Result.StatusCode = HttpStatusCode.BadRequest or 
 actual.Result.StatusCode = 500

I want to compare the return value from controller execution whether that fulfill the condition or not. From debugging I noticed the 'StatusCode' but not got an way to catch that value for comparing. My Code for testing is

    public void PostAmount_Withdraw_Returns_Null()
    {
        // Arrange
        Amount amount = new Amount() { Id = 0, PaymentDate = DateTime.Today, 
             PaymentTypeId = 3, PaymentAmount = 500, ClientId = 1, Balance = 0 };

        _accountServiceMock.Setup(s => s.Withdraw(amount)).Throws(new Exception());
        
        var target = SetupAmountsController();

        //Act
        var actual = target.PostAmount(amount);

        //Assert

        actual.ShouldNotBeNull();
        //actual.Value.ShouldBe(response);
    }

What call controller methods below : -

    public ActionResult<Amount> PostAmount(Amount amount)
    {
        try
        {
            if (amount.PaymentTypeId == 1)
            {
                _accountService.Deposit(amount);
            }
            else if (amount.PaymentTypeId == 2)
            {
                _accountService.Withdraw(amount);
            }
            else
            {
                return Problem("Model 'Amounts'  is null.");
            }
            return new OkObjectResult(new { Message = "Payment Success!" });
        }
        catch (DbUpdateConcurrencyException)
        {
            return Problem("There was error for updating model Payment");
        }
    }

And, returned the value after execution from Unit Test
enter image description here

I want to give condition likes below: -

 actual.Result.StatusCode = HttpStatusCode.BadRequest or 
 actual.Result.StatusCode = 500

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

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

发布评论

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

评论(1

握住你手 2025-02-08 20:12:48

刚得到答案。下面将喜欢以下内容: -

 (actual.Result as ObjectResult).StatusCode.ShouldBe((int)HttpStatusCode.InternalServerError);

您需要在此处定义“状态代码”要比较的内容。就我而言,是500。

Just got the answer. It will be likes below: -

 (actual.Result as ObjectResult).StatusCode.ShouldBe((int)HttpStatusCode.InternalServerError);

you need to define here 'status code' what want to compare. In my case it was 500.

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