Ads

Web API 14 - Effective Strategies for Unit Testing ASP.NET Core Web API Endpoints

In the realm of ASP.NET Core Web API development, ensuring the reliability and functionality of your endpoints is paramount. One proven approach to achieving this is through the implementation of robust unit tests. This article will guide you through effective strategies for writing unit tests for your Web API controllers and actions, exploring the tools and frameworks that can simplify the testing process.

1. Importance of Unit Testing in Web API Development

Before diving into the testing process, let's highlight the significance of unit testing in Web API development. Unit tests play a crucial role in identifying bugs early in the development cycle, ensuring code correctness, and facilitating code maintenance. For Web API endpoints, unit tests help validate that each component functions as expected, enhancing the overall reliability and stability of the application.

2. Approach to Testing Controllers and Actions

a. Using Dependency Injection for Mocking
To effectively test controllers and actions, it's essential to leverage dependency injection (DI) for mocking dependencies. This allows you to isolate the controller logic and focus on testing specific functionalities without invoking external services or databases. Popular DI containers in ASP.NET Core, such as Microsoft.Extensions.DependencyInjection, facilitate this process.

Consider the following example where a controller depends on a service:

1. UserModel


public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public DateTime DateOfBirth { get; set; }
}


2. UserSrvice Interface


public interface IUserService
{
    List<User> GetUsers();
    User GetUserById(int id);
    void CreateUser(User newUser);
    void UpdateUser(int id, User updatedUser);
    void DeleteUser(int id);
}


3. UserService Implementation


using System;
using System.Collections.Generic;

public class UserService : IUserService
{
    private static List<User> _users = new List<User>
    {
        new User { Id = 1, Name = "John Doe",
        Email = "john.doe@example.com", DateOfBirth = new DateTime(1990, 5, 15) },
        new User { Id = 2, Name = "Jane Smith",
        Email = "jane.smith@example.com", DateOfBirth = new DateTime(1985, 8, 22) }
    };

    public List<User> GetUsers()
    {
        return _users;
    }

    public User GetUserById(int id)
    {
        return _users.Find(u => u.Id == id);
    }

    public void CreateUser(User newUser)
    {
        newUser.Id = _users.Count + 1;
        _users.Add(newUser);
    }

    public void UpdateUser(int id, User updatedUser)
    {
        var existingUser = _users.Find(u => u.Id == id);

        if (existingUser != null)
        {
            existingUser.Name = updatedUser.Name;
            existingUser.Email = updatedUser.Email;
            existingUser.DateOfBirth = updatedUser.DateOfBirth;
        }
    }

    public void DeleteUser(int id)
    {
        var userToRemove = _users.Find(u => u.Id == id);

        if (userToRemove != null)
        {
            _users.Remove(userToRemove);
        }
    }
}


4. Finally, Controller UserController


using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

[Route("api/users")]
[ApiController]
public class UserController : ControllerBase
{
    private readonly IUserService _userService;

    public UserController(IUserService userService)
    {
        _userService = userService;
    }

    [HttpGet]
    public IActionResult GetAllUsers()
    {
        var users = _userService.GetUsers();
        return Ok(users);
    }

    [HttpGet("{id}")]
    public IActionResult GetUserById(int id)
    {
        var user = _userService.GetUserById(id);

        if (user == null)
        {
            return NotFound();
        }

        return Ok(user);
    }

    [HttpPost]
    public IActionResult CreateUser([FromBody] User newUser)
    {
        if (newUser == null)
        {
            return BadRequest("Invalid user data");
        }

        _userService.CreateUser(newUser);

        return CreatedAtAction(nameof(GetUserById),
        new { id = newUser.Id }, newUser);
    }

    [HttpPut("{id}")]
    public IActionResult UpdateUser(int id, [FromBody] User updatedUser)
    {
        var existingUser = _userService.GetUserById(id);

        if (existingUser == null)
        {
            return NotFound();
        }

        _userService.UpdateUser(id, updatedUser);

        return Ok(updatedUser);
    }

    [HttpDelete("{id}")]
    public IActionResult DeleteUser(int id)
    {
        var userToRemove = _userService.GetUserById(id);

        if (userToRemove == null)
        {
            return NotFound();
        }

        _userService.DeleteUser(id);

        return NoContent();
    }
}


5. Lets add the nUnit Project and add the dependencies


dotnet add package xunit
dotnet add package Microsoft.AspNetCore.Mvc.Testing
dotnet add package Moq


Ensure that you have the required testing packages installed. You can add the following packages using NuGet Package Manager Console:

Now, let's create the unit tests for the UserController:


using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Xunit;

public class UserControllerTests
{
    [Fact]
    public void GetAllUsers_ReturnsOkResponse()
    {
        // Arrange
        var mockUserService = new Mock<IUserService>();
        mockUserService.Setup(service => service.GetUsers()).Returns(new List<User>());

        var controller = new UserController(mockUserService.Object);

        // Act
        var response = controller.GetAllUsers();

        // Assert
        var result = Assert.IsType<OkObjectResult>(response);
        var users = Assert.IsType<List<User>>(result.Value);
        Assert.NotNull(users);
    }

    [Fact]
    public void GetUserById_ReturnsUserWithCorrectId()
    {
        // Arrange
        var userId = 1;
        var mockUserService = new Mock<IUserService>();
        mockUserService.Setup(service => service.GetUserById(userId))
        .Returns(new User { Id = userId, Name = "John Doe" });

        var controller = new UserController(mockUserService.Object);

        // Act
        var response = controller.GetUserById(userId);

        // Assert
        var result = Assert.IsType<OkObjectResult>(response);
        var user = Assert.IsType<User>(result.Value);
        Assert.Equal(userId, user.Id);
    }

    [Fact]
    public void CreateUser_ReturnsCreatedResponse()
    {
        // Arrange
        var newUser = new User { Name = "New User", Email = "new.user@example.com",
        DateOfBirth = new DateTime(1995, 3, 10) };
        var mockUserService = new Mock<IUserService>();

        var controller = new UserController(mockUserService.Object);

        // Act
        var response = controller.CreateUser(newUser);

        // Assert
        var result = Assert.IsType<CreatedAtActionResult>(response);
        var createdUser = Assert.IsType<User>(result.Value);
        Assert.Equal(newUser.Name, createdUser.Name);
    }

    [Fact]
    public void UpdateUser_ReturnsOkResponse()
    {
        // Arrange
        var userId = 1;
        var updatedUser = new User { Id = userId, Name = "Updated User",
        Email = "updated.user@example.com", DateOfBirth = new DateTime(1988, 6, 25) };
        var mockUserService = new Mock<IUserService>();
        mockUserService.Setup(service => service.GetUserById(userId)).
        Returns(new User { Id = userId, Name = "John Doe" });

        var controller = new UserController(mockUserService.Object);

        // Act
        var response = controller.UpdateUser(userId, updatedUser);

        // Assert
        var result = Assert.IsType<OkObjectResult>(response);
        var returnedUser = Assert.IsType<User>(result.Value);
        Assert.Equal(updatedUser.Name, returnedUser.Name);
    }

    [Fact]
    public void DeleteUser_ReturnsNoContentResponse()
    {
        // Arrange
        var userId = 1;
        var mockUserService = new Mock<IUserService>();
        mockUserService.Setup(service => service.GetUserById(userId)).
        Returns(new User { Id = userId, Name = "John Doe" });

        var controller = new UserController(mockUserService.Object);

        // Act
        var response = controller.DeleteUser(userId);

        // Assert
        Assert.IsType<NoContentResult>(response);
    }
}


These tests cover various scenarios for testing the UserController. Remember to adjust the namespaces and dependencies based on your project structure.

6. Running the Tests

To run the tests, use the dotnet test command in the terminal. Make sure to adjust the namespaces and dependencies based on your project structure

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !