Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get content value in Xunit when result returned in IActionResult type

I have a unit test project using Xunit and the method we are testing returns IActionResult.

I saw some people suggest using "NegotiatedContentResult" to get the content of the IActionResult but that doesn't work in Xunit.

So I wonder how to get the content value of an IActionResult in Xunit?

Test code example is provided below:

public void GetTest() {     var getTest = new ResourcesController(mockDb);      var result = getTest.Get("1");      //Here I want to convert the result to my model called Resource and     //compare the attribute Description like below.     Resource r = ?? //to get the content value of the IActionResult      Assert.Equal("test", r.Description); } 

Does anyone know how to do this in XUnit?

like image 442
Julie C. Avatar asked Sep 07 '16 20:09

Julie C.


People also ask

What is fact attribute in xUnit?

xUnit uses the [Fact] attribute to denote a parameterless unit test, which tests invariants in your code. In contrast, the [Theory] attribute denotes a parameterised test that is true for a subset of data. That data can be supplied in a number of ways, but the most common is with an [InlineData] attribute.

Which attribute is used to mark a method as test method in xUnit?

Testing a positive case This method is decorated with the Fact attribute, which tells xUnit that this is a test.

Which of the following attributes are eliminated in xUnit?

Here is the list of attributes removed from the framework: [Setup] and [TearDown] are replaced with Constructors & IDisposable. [TestFixture] is removed. [Ignore] is absorbed with Skip = parameter on [Fact]

What is OkObjectResult?

Ok(object) is a controller method that returns a new OkObjectResult(object); the OkResult class implements an ActionResult that produces an empty 200 response. the OkObjectResult class implements an ActionResult that process a 200 response with a body based on the passed object.


1 Answers

Depends on what you expect returned. From previous example you used an action like this.

[HttpGet("{id}")] public IActionResult Get(string id) {             var r = unitOfWork.Resources.Get(id);      unitOfWork.Complete();      Models.Resource result = ConvertResourceFromCoreToApi(r);      if (result == null) {         return NotFound();     } else {         return Ok(result);     }         } 

That method will either return a OkObjectResult or a NotFoundResult. If the expectation of the method under test is for it to return Ok() then you need to cast the result in the test to what you expect and then do your assertions on that

public void GetTest_Given_Id_Should_Return_OkObjectResult_With_Resource() {     //Arrange     var expected = "test";     var controller = new ResourcesController(mockDb);      //Act     var actionResult = controller.Get("1");      //Assert     var okObjectResult = actionResult as OkObjectResult;     Assert.NotNull(okObjectResult);      var model = okObjectResult.Value as Models.Resource;     Assert.NotNull(model);      var actual = model.Description;     Assert.Equal(expected, actual); } 
like image 62
Nkosi Avatar answered Sep 18 '22 06:09

Nkosi