Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Run tests

on:
push:
branches: [develop]
pull_request:
branches: [develop]

jobs:
format-check:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '9.0.x'

- name: Restore dependencies
run: dotnet restore
working-directory: ./CrowdedBackend

- name: Run dotnet test
run: dotnet test
working-directory: ./CrowdedBackend/CrowdedBackend.Tests
11 changes: 7 additions & 4 deletions CrowdedBackend/CrowdedBackend.Tests/CrowdedBackend.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
<ProjectReference Include="..\CrowdedBackend\CrowdedBackend.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CrowdedBackend\CrowdedBackend.csproj" />
<ItemGroup>
<None Update="Data\raspOutputData.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
43 changes: 43 additions & 0 deletions CrowdedBackend/CrowdedBackend.Tests/CustomWebApplicationFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using CrowdedBackend;
using CrowdedBackend.Models;
using CrowdedBackend.Services.CalculatePositions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace CrowdedBackend.Tests;

public class CustomWebApplicationFactory : WebApplicationFactory<CrowdedBackend.Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Testing");
builder.ConfigureServices(services =>
{
// Remove the existing DbContext registration
var descriptor = services.SingleOrDefault(
d => d.ServiceType == typeof(DbContextOptions<MyDbContext>));
if (descriptor != null)
{
services.Remove(descriptor);
}

// Register CircleUtils in the DI container
services.AddScoped<CircleUtils>();

// Unique DB name per test class
var dbName = $"TestDb_{GetType().Name}";

services.AddDbContext<MyDbContext>(options =>
options.UseInMemoryDatabase(dbName));

// Build and initialize DB
var sp = services.BuildServiceProvider();
using var scope = sp.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<MyDbContext>();
db.Database.EnsureDeleted(); // Clean slate
db.Database.EnsureCreated();
});
}
}
Loading
Loading