Tuesday, October 15, 2024

Part 2 - Using Unit Tests in ASP.NET Core Application (Simple)

Unit testing in ASP.NET Core is a key aspect of writing maintainable and testable applications. In this tutorial, I'll walk you through creating a simple unit test in an ASP.NET Core application using the xUnit framework.

Prerequisites

  • .NET Core SDK installed
  • Visual Studio or any preferred IDE
  • xUnit framework

Step 1: Set Up Your ASP.NET Core Project

Create a new ASP.NET Core web application:

  1. Open Visual Studio.
  2. Select Create a new project.
  3. Choose ASP.NET Core Web Application and click Next.
  4. Name the project (e.g., SampleApp).
  5. Select ASP.NET Core Empty or MVC template depending on your needs.

Step 2: Install the xUnit Testing Framework

You need to add xUnit and xUnit Runner to your test project. In Visual Studio, right-click on the solution and:

  1. Select Add > New Project.
  2. Choose xUnit Test Project and click Next.
  3. Name the project (e.g., SampleApp.Tests) and click Create.

Once the test project is created, install any additional dependencies:

  1. Open NuGet Package Manager (right-click your SampleApp.Tests project).
  2. Search and install:
    • xUnit
    • xUnit.runner.visualstudio
    • Moq (optional, for mocking objects).

Step 3: Writing a Simple Unit Test

Let's say you have a simple service in your application that calculates tax:

1
2
3
4
5
6
7
8
// TaxService.cs in SampleApp
public class TaxService
{
    public decimal CalculateTax(decimal amount, decimal rate)
    {
        return amount * rate;
    }
}

Unit Test for TaxService

In the test project, create a test class for this service:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// TaxServiceTests.cs in SampleApp.Tests
using Xunit;

public class TaxServiceTests
{
    [Fact]
    public void CalculateTax_ValidAmountAndRate_ReturnsCorrectTax()
    {
        // Arrange
        var taxService = new TaxService();
        decimal amount = 100m;
        decimal rate = 0.1m; // 10%

        // Act
        var result = taxService.CalculateTax(amount, rate);

        // Assert
        Assert.Equal(10m, result);
    }
}


Explanation:

  • [Fact]: This attribute marks the method as a test case in xUnit.
  • Arrange: Set up the test data and service.
  • Act: Call the method under test.
  • Assert: Verify that the method’s result is as expected.

Step 4: Run the Tests

To run the tests:

  1. Go to Test > Test Explorer in Visual Studio.
  2. Build the solution.
  3. The Test Explorer window should list your test. Click Run All to execute the tests.

If everything is set up correctly, your test should pass, and you'll see a green checkmark next to the test.

Step 5: Expanding Tests

You can add more tests to handle edge cases. For example, what happens when the rate is 0 or the amount is negative?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[Fact]
public void CalculateTax_ZeroRate_ReturnsZero()
{
    var taxService = new TaxService();
    var result = taxService.CalculateTax(100m, 0m);
    Assert.Equal(0m, result);
}

[Fact]
public void CalculateTax_NegativeAmount_ReturnsNegativeTax()
{
    var taxService = new TaxService();
    var result = taxService.CalculateTax(-100m, 0.1m);
    Assert.Equal(-10m, result);
}


Step 6: Mocking Dependencies (Optional)

If your service has dependencies, you can use Moq to mock those. For example, if the TaxService depends on an ILogger, you can mock it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
using Moq;
using Microsoft.Extensions.Logging;

[Fact]
public void CalculateTax_UsesLogger()
{
    var loggerMock = new Mock<ILogger<TaxService>>();
    var taxService = new TaxService(loggerMock.Object);
    var result = taxService.CalculateTax(100m, 0.1m);

    // Verify logger was used
    loggerMock.Verify(logger => logger.Log(
        It.IsAny<LogLevel>(),
        It.IsAny<EventId>(),
        It.IsAny<object>(),
        It.IsAny<Exception>(),
        It.IsAny<Func<object, Exception, string>>()
    ));
}

No comments:

Post a Comment