Answer the question
In order to leave comments, you need to log in
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();
}
[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);
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question