N
N
Nikita072021-07-15 12:49:56
C++ / C#
Nikita07, 2021-07-15 12:49:56

Why doesn't the unit test result match what I expect?

Hello everyone, help me figure out the Unit test, there is the following controller

[HttpGet]
        public async Task<ActionResult> Auction([FromQuery] int[] ids, [FromQuery] string platfrom, AuctionModel auctionModel)
        {
            var partners = _adcampaign.GetRtbPartners(ids);
            var result = await _rtbservice.GetAuctionResult(partners.Values, auctionModel);

            if (result?.Length > 0)
            {
                return new ContentResult
                {
                    Content = result,
                    ContentType = "application/xml",
                    StatusCode = 200
                };
            }
            else return NoContent();
        }


And the following unit-test was written for it
[Fact]
        public async Task ReturnNoContentResult()
        {
            int[] ids = // Заполение

            IReadOnlyDictionary<int, RtbModel> partners = // Заполение

            List<RtbModel> rtb_partners = // Заполнение

            var auctionModel = // Заполнение

            // Arrange
            var mock1 = new Mock<IRtbService>();
            mock1
                .Setup(a => a.GetAuctionResult(rtb_partners, auctionModel))
                .ReturnsAsync("Строка");

            var mock2 = new Mock<ICachingResults>();
            mock2
                .Setup(a => a.GetRtbPartners(ids))
                .Returns(partners);

            var controller = new RtbController(mock1.Object, mock2.Object);

            // Act
            var result = await controller.Auction(ids, "1111-1-1", auctionModel);

            // Assert
            var contentResult = Assert.IsType<OkResult>(result);
        }


There is the following error while executing
60f00487d0d03019268014.png

Why does the test expect NoContentResult when it should return a string?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2021-07-15
@Nikita07

The controller method returns an ActionResult type.
It is the parent of OkResult, NoContentResult, ContentResult and others.
This Assert.IsType(result) checks for type compliance and not for the fact that the response will be 200.
The type should have been ContentResult and inside both the code and the string.
But you return NoContentResult. Apparently, when configuring the test, you made a mistake and you have
result?.Length > 0 == false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question