ASP.NET Core 6 permits us to develop APIs that include a bare minimum of documents, functions, as well as reliances. Here’s exactly how to test a very little Web API.
ASP.NET Core 6 presents a simplified hosting version that can be made use of to apply lightweight APIs by getting rid of the need to compose the boilerplate code called for in previous variations ASP.NET Core.
We went over how to begin with very little APIs, and how to make use of logging and reliance injection with minimal APIs, in previous short articles below as well as right here. This post reviews exactly how we can examine minimal APIs in ASP.NET Core 6.
To deal with the code instances given in this post, you should have Visual Studio 2022 installed in your system. If you do not currently have a duplicate, you can download Visual Studio 2022 here.
Develop an ASP.NET Core Web API project in Visual Studio 2022
To begin with, allow’s create an ASP.NET Core Internet API project in Visual Studio 2022. Adhering to these actions will certainly create a new Web API project in Visual Workshop 2022:
- Introduce the Visual Studio 2022 IDE.
- Click on “Develop new task.”
- In the “Develop brand-new task” window, select “ASP.NET Core Web API” from the listing of layouts presented.
- Click Next.
- In the “Configure your brand-new project” home window, define the name and also place for the brand-new project.
- Additionally check the “Location remedy as well as task in the very same directory” check box, relying on your preferences.
- Click Next.
- In the “Extra Details” home window revealed next, uncheck the check box that claims “Usage controllers …” because we’ll be using minimal APIs in this instance. Leave the “Verification
- Kind” as “None” (default).
- Make sure that the check boxes “Enable Docker,” “Set up for HTTPS,” and “Enable Open API Support” are unattended as we won’t be making use of any one of those features below.
- Click Develop.
We’ll utilize this ASP.NET Core 6 Web API task to check very little APIs in the subsequent areas of this short article.
Service framework of the complete minimal Web API application
In this example, we’ll build two applications, called MinimalAPIDemo and also MinimalAPIDemoTest. MinimalAPIDemo is the very little ASP.NET Core 6 Internet API that we’ll evaluate, and also MinimalAPITests is the test application. In our instance, MinimalAPITests will consist of one test method to examine the MinimalAPIDemo API.
Produce a very little Internet API in ASP.NET Core 6
Let’s currently develop our marginal Internet API in ASP.NET Core 6. We’ll name it CustomerAPI. This Consumer API will have the complying with files:
- Client (this stands for the model course).
- ICustomerRepository (this stands for the customer repository user interface).
- CustomerRepository (this represents the client repository class that carries out the ICustomerRepository interface).
The Customer version course.
Produce a brand-new documents called Customer.cs and also provide it the following code.
namespace MinimalAPIDemo { public class Customer { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string City { get; set; } public string Country { get; set; } public string Phone { get; set; } } }
The ICustomerRepository interface
Now create a file named ICustomerRepository.cs and insert this code.
namespace MinimalAPIDemo { public interface ICustomerRepository { public Task<List<Customer>> GetCustomers(); } }
The CustomerRepository class
Next create a file named CustomerRepository.cs and insert the following code.
namespace MinimalAPIDemo { public class CustomerRepository : ICustomerRepository { private readonly List<Customer> _authors; public CustomerRepository() { _authors = new List<Customer> { new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", Address = "ABC Hills", City = "Hyderabad", Country= "India", Phone = "0123456789" }, new Customer { Id = 2, FirstName = "Anand", LastName = "Narayanaswamy", Address = "XYZ Hills", City = "Thiruvananthapuram", Country= "India", Phone = "1234567890" }, new Customer { Id = 3, FirstName = "Charles", LastName = "Fisher", Address = "Dawson Road", City = "New York ", Country= "US", Phone = "1234567890" } }; } public async Task<List<Customer>> GetCustomers() { return await Task.FromResult(_authors); } } }
The Program.cs file
Write the following code in the Program.cs file to create the endpoint.
app.MapGet("/customers", async (ICustomerRepository customerRepository) => await customerRepository.GetCustomers());
For simpleness, we’ll produce simply one endpoint in this example. Add an instance of kind ICustomerRepository as a scoped solution as shown below.
builder.Services.AddScoped<ICustomerRepository, CustomerRepository>();
You should also include a partial class named Program. This is due to the fact that the Program.cs documents will be put together right into a personal class Program, which can not be accessed from beyond the setting up.
public partial class Program { }
This partial course will make the Program course obtainable to any task that referrals this setting up. The total source code of the Program.cs documents is offered below for your referral.
using MinimalAPIDemo; var builder = WebApplication.CreateBuilder(args); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddScoped<ICustomerRepository, CustomerRepository>(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.MapGet("/customers", async (ICustomerRepository customerRepository) => await customerRepository.GetCustomers()); app.Run(); public partial class Program { }
Create a very little Internet API examination project in ASP.NET Core 6.
Develop a Xunit examination task called MinimalAPIDemo.Tests as well as rename the default unit examination data to MinimalAPITests.cs. Right here is where you must write your examination approaches. In this instance, we’ll create only one examination technique to check the endpoint we developed earlier.
Now allow’s create a test technique called GetAllCustomersTest with the adhering to code.
[Fact] public async void GetAllCustomersTest() { await using var application = new WebApplicationFactory<Program>(); using var client = application.CreateClient(); var response = await client.GetAsync("/customers"); var data = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
The complete source code of the MinimalAPITests class is given below for your reference.
using Microsoft.AspNetCore.Mvc.Testing; using System.Net; using Xunit; namespace MinimalAPIDemo.Tests { public class MinimalAPITests { [Fact] public async void GetAllCustomersTest() { await using var application = new WebApplicationFactory<Program>(); using var client = application.CreateClient(); var response = await client.GetAsync("/customers"); var data = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); } } }
This being a minimal execution, we’ve not used a database or an information context, yet just a basic repository class with a few static data aspects– simply sufficient to highlight exactly how we can build very little APIs as well as test them in ASP.NET Core 6. I’ll have even more to state on marginal APIs in future blog posts here.