6

I am new in XUnit tests for Web API application. I am trying to make assertion of my response value with the status code 404, but I am not sure how to do so.

Here's my test code.

Assume the input is null, the test is expected to return NotFound or 404 status code.

  [Fact]
        public async Task getBooksById_NullInput_Notfound()
        {

            //Arrange
            var mockConfig = new Mock<IConfiguration>();
            var controller = new BookController(mockConfig.Object);

            //Act
            var response = await controller.GetBooksById(null);

            //Assert
            mockConfig.VerifyAll();

            Assert(response.StatusCode, HttpStatusCode.NotFound()); //How to achieve this?

        }

In the last line of this test method, I am not able to make response.StatusCode compile as in the response variable does not have StatusCode property. And there is no HttpStatusCode class that I can call...

Here's my GetBooksByID() method in the controller class.

 public class BookController : Controller
    {
        private RepositoryFactory _repositoryFactory = new RepositoryFactory();

        private IConfiguration _configuration;

        public BookController(IConfiguration configuration)
        {
            _configuration = configuration;
        }



        [HttpGet]
        [Route("api/v1/Books/{id}")]
        public async Task<IActionResult> GetBooksById(string id)
        {
            try
            {
                if (string.IsNullOrEmpty(id))
                {
                    return BadRequest();
                }

                var response = //bla

                //do something

                if (response == null)
                {
                    return NotFound();
                }
                return new ObjectResult(response);
            }
            catch (Exception e)
            {
                throw;
            }
        }

Thanks!

2
  • 1
    It won't compile because HttpStatusCode.NotFound() is not a method. Remove the parenthesis e.g. HttpStatusCode.NotFound. Commented Apr 19, 2018 at 6:51
  • @Brad thx! It works now :) Commented Apr 19, 2018 at 7:00

4 Answers 4

7

You can check against returned type

// Act
var actualResult = await controller.GetBooksById(null);

// Assert
actualResult.Should().BeOfType<BadRequestResult>();

Should() and .BeOfType<T> are methods from FluentAssertions library, which available on Nuget

Sign up to request clarification or add additional context in comments.

3 Comments

My bad, I didnt use using FluentAssertions;
sry, but NotFound is not compile, am I missing any other package?
@Brad, this was an alternative approach to test for expected response, what OP actually tried to do. Seems approach suit OP.
3

Assert.Equal(System.Net.HttpStatusCode.Unauthorized, response.StatusCode);

Comments

2

Old question, but while the accepted answer is valid it pulls in another dependency that's helpful but not required to do this. The most straight forward approach is to reference:

using Microsoft.AspNetCore.Mvc;

Then test with:

Assert.IsAssignableFrom<NotFoundObjectResult>(response.Result);

assuming your method returns an ActionResult.

Comments

-1

After getting hints by @Brad, the code compiles and passed the test.

 response.ToString().Equals(HttpStatusCode.NotFound);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.