Skip to content

Commit 4581270

Browse files
verbedrDries Verbeke
andauthored
Setup unit test skeleton (#30)
Co-authored-by: Dries Verbeke <verbedr@cronos.be>
1 parent 596b5d5 commit 4581270

File tree

8 files changed

+148
-7
lines changed

8 files changed

+148
-7
lines changed

Fritz.InstantAPIs.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1818
CONTRIBUTING.md = CONTRIBUTING.md
1919
EndProjectSection
2020
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{CD123B01-1B52-4E80-84F7-4D10E01EE10F}"
22+
EndProject
2123
Global
2224
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2325
Debug|Any CPU = Debug|Any CPU
@@ -32,6 +34,10 @@ Global
3234
{C945FAF9-D8A9-48EE-8A19-4AB0996CABEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
3335
{C945FAF9-D8A9-48EE-8A19-4AB0996CABEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
3436
{C945FAF9-D8A9-48EE-8A19-4AB0996CABEC}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{CD123B01-1B52-4E80-84F7-4D10E01EE10F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{CD123B01-1B52-4E80-84F7-4D10E01EE10F}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{CD123B01-1B52-4E80-84F7-4D10E01EE10F}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{CD123B01-1B52-4E80-84F7-4D10E01EE10F}.Release|Any CPU.Build.0 = Release|Any CPU
3541
EndGlobalSection
3642
GlobalSection(SolutionProperties) = preSolution
3743
HideSolutionNode = FALSE

Fritz.InstantAPIs/MapApiExtensions.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.AspNetCore.Routing;
34
using System.ComponentModel.DataAnnotations;
45
using System.Reflection;
56

@@ -10,7 +11,7 @@ internal class MapApiExtensions
1011

1112
// TODO: Authentication / Authorization
1213

13-
internal static void MapInstantGetAll<D, C>(WebApplication app, string url)
14+
internal static void MapInstantGetAll<D, C>(IEndpointRouteBuilder app, string url)
1415
where D : DbContext where C : class
1516
{
1617

@@ -21,7 +22,7 @@ internal static void MapInstantGetAll<D, C>(WebApplication app, string url)
2122

2223
}
2324

24-
internal static void MapGetById<D,C>(WebApplication app, string url)
25+
internal static void MapGetById<D,C>(IEndpointRouteBuilder app, string url)
2526
where D: DbContext where C : class
2627
{
2728

@@ -48,7 +49,7 @@ internal static void MapGetById<D,C>(WebApplication app, string url)
4849

4950
}
5051

51-
internal static void MapInstantPost<D, C>(WebApplication app, string url)
52+
internal static void MapInstantPost<D, C>(IEndpointRouteBuilder app, string url)
5253
where D : DbContext where C : class
5354
{
5455

@@ -61,7 +62,7 @@ internal static void MapInstantPost<D, C>(WebApplication app, string url)
6162

6263
}
6364

64-
internal static void MapInstantPut<D, C>(WebApplication app, string url)
65+
internal static void MapInstantPut<D, C>(IEndpointRouteBuilder app, string url)
6566
where D : DbContext where C : class
6667
{
6768

@@ -75,7 +76,7 @@ internal static void MapInstantPut<D, C>(WebApplication app, string url)
7576

7677
}
7778

78-
internal static void MapDeleteById<D, C>(WebApplication app, string url)
79+
internal static void MapDeleteById<D, C>(IEndpointRouteBuilder app, string url)
7980
where D : DbContext where C : class
8081
{
8182

Fritz.InstantAPIs/WebApplicationExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Reflection;
1+
using Microsoft.AspNetCore.Routing;
2+
using System.Reflection;
23

34
namespace Microsoft.AspNetCore.Builder;
45

@@ -7,7 +8,7 @@ public static class WebApplicationExtensions
78

89
public static InstantAPIsConfig Configuration { get; set; } = new();
910

10-
public static WebApplication MapInstantAPIs<D>(this WebApplication app, Action<InstantAPIsConfig> configAction = null) where D: DbContext
11+
public static IEndpointRouteBuilder MapInstantAPIs<D>(this IEndpointRouteBuilder app, Action<InstantAPIsConfig> configAction = null) where D: DbContext
1112
{
1213

1314
if (configAction != null) configAction(Configuration);

Test/BaseFixture.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Moq;
2+
3+
namespace Test;
4+
5+
public abstract class BaseFixture
6+
{
7+
public BaseFixture()
8+
{
9+
Mockery = new MockRepository(MockBehavior.Loose);
10+
}
11+
12+
protected MockRepository Mockery { get; private set; }
13+
}

Test/GlobalSurpressions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
// This file is used by Code Analysis to maintain SuppressMessage
3+
// attributes that are applied to this project.
4+
// Project-level suppressions either have no target or are given
5+
// a specific target and scoped to a namespace, type, member, etc.
6+
7+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Readability", "RCS1018:Add default access modifier.", Justification = "<Pending>")]
8+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Maintainability", "RCS1141:Add parameter to documentation comment.", Justification = "<Pending>")]
9+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Redundancy", "RCS1163:Unused parameter.", Justification = "<Pending>")]
10+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "RCS1090:Call 'ConfigureAwait(false)'.", Justification = "No SynchronizationContext in AspNet core")]
11+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0007:Use implicit type", Justification = "Dont want to use var for bool, int ...")]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using Microsoft.AspNetCore.Routing;
3+
using Microsoft.EntityFrameworkCore;
4+
using System.Collections.Generic;
5+
using Xunit;
6+
7+
namespace Test.InstantAPIs;
8+
9+
public class WebApplciationExtensions : BaseFixture
10+
{
11+
12+
[Fact]
13+
public void WhenMapInstantAPIsExpectedDefaultBehaviour()
14+
{
15+
16+
// arrange
17+
var app = Mockery.Create<IEndpointRouteBuilder>();
18+
var dataSources = new List<EndpointDataSource>();
19+
app.Setup(x => x.DataSources).Returns(dataSources);
20+
21+
// act
22+
app.Object.MapInstantAPIs<MyContext>();
23+
24+
// assert
25+
Assert.NotEmpty(dataSources);
26+
}
27+
28+
29+
private class MyContext : DbContext
30+
{
31+
32+
public MyContext(DbContextOptions<MyContext> options) : base(options) { }
33+
34+
public DbSet<Contact> Contacts => Set<Contact>();
35+
36+
}
37+
38+
private class Contact
39+
{
40+
41+
public int Id { get; set; }
42+
public string? Name { get; set; }
43+
public string? Email { get; set; }
44+
45+
}
46+
47+
}

Test/Test.csproj

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="FluentAssertions" Version="6.5.1" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
13+
<PackageReference Include="Moq" Version="4.16.1" />
14+
<PackageReference Include="xunit" Version="2.4.1" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
16+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17+
<PrivateAssets>all</PrivateAssets>
18+
</PackageReference>
19+
<PackageReference Include="coverlet.collector" Version="3.1.0">
20+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
21+
<PrivateAssets>all</PrivateAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\Fritz.InstantAPIs\Fritz.InstantAPIs.csproj" />
27+
</ItemGroup>
28+
29+
</Project>

Test/XunitLogger.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
using Xunit.Abstractions;
4+
5+
namespace Test;
6+
7+
public class XunitLogger<T> : ILogger<T>, IDisposable
8+
{
9+
private ITestOutputHelper _output;
10+
11+
public XunitLogger(ITestOutputHelper output)
12+
{
13+
_output = output;
14+
}
15+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
16+
{
17+
_output.WriteLine(state.ToString());
18+
}
19+
20+
public bool IsEnabled(LogLevel logLevel)
21+
{
22+
return true;
23+
}
24+
25+
public IDisposable BeginScope<TState>(TState state)
26+
{
27+
return this;
28+
}
29+
30+
public void Dispose()
31+
{
32+
}
33+
}

0 commit comments

Comments
 (0)