1

I came to know that through FluentAssertions library we can combine multiple assertion in a single like. Just want to know if below 2 assert can be combined in a single line?

        // Act 
        IActionResult actionResult = controller.Update();

        // Assert
        ((ObjectResult)actionResult).StatusCode.Should().Be(200);
        ((ObjectResult)actionResult).Value.Should().BeEquivalentTo("updated");
user584018
  • 10,186
  • 15
  • 74
  • 160
  • 1
    You can chain validations on a single property like `statusCode.Should().BeGreaterOrEqualTo(200).And.BeLessOrEqualTo(206);` but I don't think you can chain validation for multiple properties on a single line. – Michal Diviš Jan 17 '22 at 09:47

1 Answers1

2

With the built-in assertions you can compare actionResult against an anonymous object.

IActionResult actionResult = new ObjectResult("updated")
{
    StatusCode = 200
};

var expected = new
{
    StatusCode = 200,
    Value = "updated"
};

actionResult.Should().BeEquivalentTo(expected);

For you're specific case you can install FluentAssertions.AspNetCore.Mvc, which lets you write

actionResult.Should().BeObjectResult()
    .WithStatusCode(200)
    .WithValue("updated");

If you're using Microsoft.AspNet.Mvc and not Microsoft.AspNetCore.Mvc there's

Jonas Nyrup
  • 2,376
  • 18
  • 25
  • Appreciate and Thanks for this awesome tip. :) I have other question, would you mind have a look there https://stackoverflow.com/questions/70721518/how-to-write-unit-test-for-program-and-startup-cs-file-for-asp-net-core-web-api – user584018 Jan 17 '22 at 14:58