Previously a basic API for Sights and facts was created ASP Net Rest API – Treasure Hunt – Guess checking.
Now let’s automate “playing” with the API by using and writing Unit Tests.
Create a new Project for the unit tests and add existing Server Project as dependency.
dotnet new xunit -o Tests
cd Tests
dotnet add reference ..\Server
dotnet add package Microsoft.AspNetCore.Mvc.Hosting
dotnet add package Microsoft.AspNetCore.Mvc.Testing
dotnet sln add Tests\
In the newly created project add a factory to setup the API and allow seeding the application or DB with test dummy data later.
using System;
using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
// https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/test/integration-tests/samples/3.x/IntegrationTestsSample/tests/RazorPagesProject.Tests/CustomWebApplicationFactory.cs
namespace Tests.IntegrationTests
{
public class TestingWebAppFactory<TEntryPoint> : WebApplicationFactory<Program> where TEntryPoint : Program
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
// create and seed test DB here
}
}
}
And first basic tests of Sight API. That read the list of Sights and ensures, it’s parseable into Sight Objects, meaning the format is correct JSON, structure matches Sight and member Types and minimum data is included.
As well as testing the guess distance check, by inserting different locations and checking if it detects if guessed correctly or not.
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using tryout_blazor_api.Shared;
using Xunit;
namespace Tests.IntegrationTests
{
public class SightTests: IClassFixture<TestingWebAppFactory<Program>>
{
private readonly HttpClient _client;
public SightTests(TestingWebAppFactory<Program> factory)
=> _client = factory.CreateClient();
[Fact]
public async Task Sights()
{
// Arrange
// Act
var response = await _client.GetAsync("/Sight");
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var sights = JsonSerializer.Deserialize<List<Sight>>(responseString);
// Assert
Assert.Equal(sights.Count, 2);
}
[Theory]
[InlineData(0, 0, 0, "Wrong guess")]
[InlineData(1, 0, 0, "Wrong guess")]
[InlineData(1, 52.514444F, 13.35F, "guessed it")]
[InlineData(1, 52.514444F, 13.05F, "Wrong guess")]
[InlineData(1, 52.414444F, 13.35F, "Wrong guess")]
[InlineData(0, 52.514444F, 13.35F, "Wrong guess")]
[InlineData(0, 52.516272F, 13.377722F, "guessed it")]
public async Task Sight_Distance(int sightId, float lat, float lon, string result)
{
// Arrange
Location loc = new() { Latitude = lat, Longitude = lon };
// Act
var response = await _client.PostAsJsonAsync($"/Sight/check/{sightId}", loc);
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
// Assert
Assert.Contains(result, responseString);
}
}
}
Code can be found here: https://github.com/sukapx/tryout_blazor_api/tree/f0df982b8419a2b0a045c348c3a24a8a0a7f7bf0
Thanks to: