This commit is contained in:
2024-12-30 21:25:35 +01:00
parent fba9aa9349
commit a46961cf80
19 changed files with 406 additions and 11 deletions

2
tests/Solar.Api.Tests/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
bin/
obj/

View File

@@ -0,0 +1,20 @@
using Solar.Api.Models;
namespace Solar.Api.Tests.Controllers;
[Collection("Default")]
public class SolarLogController
{
private readonly DefaultWebApplicationFactory applicationFactory;
public SolarLogController(DefaultWebApplicationFactory applicationFactory)
{
this.applicationFactory = applicationFactory;
}
[Fact]
public void GetDayDetails_returns_for_existing_data()
{
var client = applicationFactory.CreateClient();
}
}

View File

@@ -0,0 +1,6 @@
namespace Solar.Api.Tests;
[CollectionDefinition("Default")]
public class DefaultCollectionDefinition : ICollectionFixture<DefaultWebApplicationFactory>
{
}

View File

@@ -0,0 +1,30 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Solar.Api.Tests;
public class DefaultWebApplicationFactory : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
services.AddDbContext<DatabaseContext>(options =>
{
options.UseSqlite("Data Source=test.db");
});
});
builder.ConfigureServices(services =>
{
using var provider = services.BuildServiceProvider();
using var scope = provider.CreateScope();
using var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
databaseContext.Database.EnsureCreated();
});
}
}

View File

@@ -0,0 +1,18 @@
using System.Text.Json;
using Shouldly;
namespace Solar.Api.Tests.Extensions;
public static class HttpClientExtensions
{
public static async Task<T> FetchJson<T>(this HttpClient client, string path) where T : class
{
var response = await client.GetAsync(path);
response.EnsureSuccessStatusCode();
var result = JsonSerializer.Deserialize<T>(await response.Content.ReadAsStringAsync());
result.ShouldNotBeNull();
return result;
}
}

View File

@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="7.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Shouldly" Version="4.1.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Solar.Api\Solar.Api.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,2 @@
global using Xunit;
global using Solar.Api.Tests.Extensions;

Binary file not shown.