2

I am new at implementing unit tests and integration tests. I am trying to write some integration tests for my application. Following are the code snippets from my application to give you all the idea of my code. It would be great help if you could provide me some guidance for it.

namespace MyApplication.ApiControllers
{
    [Authorize]
    [RoutePrefix("api/customers")]
    [AppExceptionFilter]
    public class CustomersController : ApiController
    {
        private readonly IMediator _mediator;

        public CustomersController(IMediator mediator)
        {
           _mediator = mediator;
        }

        [HttpGet]
        [Route("GetCustomer")]
        public async Task<IHttpActionResult> GetCustomer(string customerNumber, string customerType = null)
        {
            var result = await _mediator.RequestAsync(new GetCustomerRequest(customerNumber, customerType));
            return Ok(result);
        }
    }
}

Following is the implementation for GetCustomerRequest handler

public async Task<List<Customer>> HandleAsync(GetCustomerRequest request)
{
    var result = await customerService.GetCustomer(request.CustomerNumber, request.CustomerType);
    // some business logic 
    return result;
}

Following is the implementation for customerService

public async Task<List<Customer>> GetCustomer(string customerNumber, string customerType = null)
{

    using (var dataContext = _dataContextFactory.Invoke())
    {
        result = await dataContext.Customers
            .Where(b => b.CustomerNumber == customerNumber)
            .Where(b => b.CustomerType == customerType)
            .Select(b => new Customer
            {
                // Properties assignment...
            })
            .ToListAsync();
    }

    return result;
}

Below is the integration unit test what I have tried.

namespace MyApplication.Tests.Integrations
{
    [TestFixture]
    public class CustomersControllerTests
    {
        private string _baseAddress;
        private string _username;
        private string _password;
        private IApiClient _apiClient;

        [SetUp]
        public void Setup()
        {
            _baseAddress = "https://mywebaaplication.com"; // TODO get this from a config
            _username = "";
            _password = "";
            _apiClient = new ApiClient(new ApiClientAuthenticationHandler(), _baseAddress);  // REPLACE with AzureADApiClientAuthenticationHandler
        }

        [Test]
        public async Task CustomersController_GetCustomer()
        {
            var customerNumber = string.Empty;
            var customerType = 500;
            var result = await _apiClient.GetAsync<Customer[]>($"/api/customers/GetCustomer?customerNumber={customerNumber}&customerType={customerType}");
            Assert.IsNotNull(result);
            Assert.IsTrue(result?.Length > 0);
        }
    }
}

1 Answers1

0

You can do a few things:

  1. Create a webhost within your unit test, then do http requests against it
  2. Not test your controller in a unit test, but in a liveness/readiness check (because it's just glue code anyway). Just do integration testing for your service.
  3. Just test against "new CustomersController"

There isn't a right/wrong answer here. You just look at the risks, and test accordingly. Also depends on the type of code-changes you expect. Sometimes its fine to create the test only within the context of a new change, no need to anticipate everything.

Wouter Schut
  • 907
  • 2
  • 10
  • 22