Skip to content

Commit 60b883e

Browse files
feat: implement demo tests
1 parent 51550ed commit 60b883e

File tree

7 files changed

+61
-34
lines changed

7 files changed

+61
-34
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("CanBeYours.Application.Tests.Unit")]
4+
[assembly: InternalsVisibleTo("CanBeYours.Application.Tests.Integration")]
5+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]

src/3-Tests/Application.Tests.Integration/Application.Tests.Integration.csproj

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
<AssemblyName>CanBeYours.Application.Tests.Integration</AssemblyName>
77
<RootNamespace>CanBeYours.Application.Tests.Integration</RootNamespace>
8-
<Nullable>enable</Nullable>
98
<IsPackable>false</IsPackable>
109
<IsTestProject>true</IsTestProject>
1110
</PropertyGroup>
@@ -18,6 +17,12 @@
1817
<PackageReference Include="CodeBlock.DevKit.Clients.AdminPanel" Version="1.3.7" />
1918
</ItemGroup>
2019

20+
<ItemGroup>
21+
<Content Include="..\..\..\codeblock.dev.license.lic">
22+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23+
</Content>
24+
</ItemGroup>
25+
2126
<ItemGroup>
2227
<ProjectReference Include="..\..\1-Libraries\Application\Application.csproj" />
2328
<ProjectReference Include="..\..\1-Libraries\Infrastructure\Infrastructure.csproj" />

src/3-Tests/Application.Tests.Integration/Fixtures/TestsBaseFixture.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.AspNetCore.Builder;
88
using Microsoft.Extensions.Configuration;
99
using Microsoft.Extensions.DependencyInjection;
10-
using Microsoft.Extensions.DependencyInjection.Extensions;
1110
using Microsoft.Extensions.Logging;
1211
using NSubstitute;
1312

@@ -19,16 +18,16 @@ public abstract class TestsBaseFixture : IntegrationTestsBase
1918
public readonly IMapper _mapper;
2019
public readonly IDemoThingRepository _demoThingRepository;
2120
public readonly ICurrentUser _currentUser;
22-
public readonly ILogger _logger;
2321

2422
protected TestsBaseFixture(string dbNameSuffix)
2523
: base(dbNameSuffix)
2624
{
2725
_demoThingRepository = GetRequiredService<IDemoThingRepository>();
2826
_mapper = GetRequiredService<IMapper>();
29-
_currentUser = GetRequiredService<ICurrentUser>();
30-
_logger = GetRequiredService<ILogger>();
3127
_requestDispatcher = Substitute.For<IRequestDispatcher>();
28+
29+
_currentUser = Substitute.For<ICurrentUser>();
30+
_currentUser.GetUserId().Returns(Guid.NewGuid().ToString());
3231
}
3332

3433
/// <summary>
@@ -60,32 +59,38 @@ public async Task<DemoThing> GetDemoThingAsync(string id)
6059
return await _demoThingRepository.GetByIdAsync(id);
6160
}
6261

62+
public new T GetRequiredService<T>()
63+
{
64+
return _serviceProvider.GetRequiredService<T>();
65+
}
66+
6367
/// <summary>
6468
///
6569
/// </summary>
6670
public override IServiceProvider GetServiceProvider(string dbNameSuffix)
6771
{
68-
var services = new ServiceCollection();
72+
var builder = WebApplication.CreateBuilder();
6973

70-
var configuration = new ConfigurationBuilder()
71-
//Copy from AdminPanel during the build event
72-
.AddJsonFile("appsettings.json", reloadOnChange: true, optional: false)
74+
// Configure the test configuration
75+
builder
76+
.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
7377
.AddJsonFile("appsettings.Development.json", optional: true)
74-
.AddInMemoryCollection([new KeyValuePair<string, string?>("MongoDB:DatabaseName", $"Test_DemoThings_DB_{dbNameSuffix}")])
75-
.Build();
78+
.AddInMemoryCollection([new("MongoDB:DatabaseName", $"Test_DemoThings_DB_{dbNameSuffix}")]);
7679

77-
services.AddSingleton<IConfiguration>(provider =>
80+
// Add logging services
81+
builder.Services.AddLogging(logging =>
7882
{
79-
return configuration;
83+
logging.AddConsole();
84+
logging.SetMinimumLevel(LogLevel.Debug);
8085
});
8186

82-
var builder = WebApplication.CreateBuilder();
83-
84-
builder.Services.Add(services);
85-
87+
// Register AdminPanel client module
8688
builder.AddAdminPanelClientModule(typeof(TestsBaseFixture));
89+
90+
// Register infrastructure services first
8791
builder.Services.AddInfrastructureModule();
8892

89-
return services.BuildServiceProvider();
93+
// Build the service provider
94+
return builder.Services.BuildServiceProvider();
9095
}
9196
}

src/3-Tests/Application.Tests.Integration/UseCases/DemoThings/CreateDemoThingTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
using CanBeYours.Application.Tests.Integration.Fixtures;
2+
using CanBeYours.Application.UseCases.DemoThings.CreateDemoThing;
23
using CanBeYours.Core.Domain.DemoThings;
34
using FluentAssertions;
5+
using Microsoft.Extensions.Logging;
46

57
namespace CanBeYours.Application.Tests.Integration.UseCases.DemoThings;
68

79
[Collection(nameof(DemoThingsCollectionFixture))]
810
public class CreateDemoThingTests
911
{
1012
private readonly DemoThingsCollectionFixture _fixture;
13+
private readonly ILogger<CreateDemoThingUseCase> _logger;
1114

1215
public CreateDemoThingTests(DemoThingsCollectionFixture fixture)
1316
{
1417
_fixture = fixture;
18+
_logger = _fixture.GetRequiredService<ILogger<CreateDemoThingUseCase>>();
1519
}
1620

1721
[Fact]
18-
public async Task DemoThing_is_added()
22+
public async Task DemoThing_is_created()
1923
{
2024
//Arrange
2125
var request = new CreateDemoThingRequest(name: "Test Name", description: "Test Description", type: DemoThingType.DemoType1);
2226
var createDemoThingUseCase = new CreateDemoThingUseCase(
2327
_fixture._demoThingRepository,
2428
_fixture._requestDispatcher,
25-
_fixture._logger,
29+
_logger,
2630
_fixture._currentUser
2731
);
2832

src/3-Tests/Application.Tests.Unit/Application.Tests.Unit.csproj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
6-
<Nullable>enable</Nullable>
7-
6+
<AssemblyName>CanBeYours.Application.Tests.Unit</AssemblyName>
7+
<RootNamespace>CanBeYours.Application.Tests.Unit</RootNamespace>
88
<IsPackable>false</IsPackable>
99
<IsTestProject>true</IsTestProject>
1010
</PropertyGroup>
1111

12+
<ItemGroup>
13+
<Content Include="..\..\..\codeblock.dev.license.lic">
14+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
15+
</Content>
16+
</ItemGroup>
17+
1218
<ItemGroup>
1319
<PackageReference Include="coverlet.collector" Version="6.0.0" />
1420
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />

src/3-Tests/Application.Tests.Unit/Fixtures/TestsBaseFixture.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
using CanBeYours.Core.Domain.DemoThings;
2-
using Castle.Core.Logging;
32
using CodeBlock.DevKit.Application.Srvices;
43
using CodeBlock.DevKit.Test.TestBase;
54
using NSubstitute;
65

7-
namespace Application.Tests.Unit.Fixtures;
6+
namespace CanBeYours.Application.Tests.Unit.Fixtures;
87

98
public abstract class TestsBaseFixture : UnitTestsBase
109
{
11-
protected IRequestDispatcher? RequestDispatcher;
12-
protected ILogger? Logger;
13-
protected IDemoThingRepository? DemoThingRepository;
10+
protected IRequestDispatcher RequestDispatcher;
11+
protected IDemoThingRepository DemoThingRepository;
1412
protected List<DemoThing> DemoThings = new List<DemoThing>();
15-
protected ICurrentUser? CurrentUser;
13+
protected ICurrentUser CurrentUser;
1614

1715
protected override void FixtureSetup()
1816
{
@@ -24,8 +22,8 @@ protected override void FixtureSetup()
2422
private void CommonFixtureSetup()
2523
{
2624
RequestDispatcher = Substitute.For<IRequestDispatcher>();
27-
Logger = Substitute.For<ILogger>();
2825
CurrentUser = Substitute.For<ICurrentUser>();
26+
CurrentUser.GetUserId().Returns(Guid.NewGuid().ToString());
2927

3028
DemoThings = GenerateDemoThingsList();
3129

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using Application.Tests.Unit.Fixtures;
1+
using CanBeYours.Application.Tests.Unit.Fixtures;
2+
using CanBeYours.Application.UseCases.DemoThings.CreateDemoThing;
23
using CanBeYours.Core.Domain.DemoThings;
34
using FluentAssertions;
5+
using Microsoft.Extensions.Logging;
46
using NSubstitute;
57

6-
namespace Application.Tests.Unit.UseCases.DemoThings;
8+
namespace CanBeYours.Application.Tests.Unit.UseCases.DemoThings;
79

810
public class CreateDemoThingTests : TestsBaseFixture
911
{
@@ -12,13 +14,13 @@ public class CreateDemoThingTests : TestsBaseFixture
1214
public CreateDemoThingTests() { }
1315

1416
[Fact]
15-
public async Task DemoThing_is_added()
17+
public async Task DemoThing_is_created()
1618
{
1719
//Arrange
1820
var request = new CreateDemoThingRequest(name: "Test Name", description: "Test Description", type: DemoThingType.DemoType1);
1921

2022
//Act
21-
var result = await createDemoThingUseCase.Handle(addDemoThingRequest, CancellationToken.None);
23+
var result = await createDemoThingUseCase.Handle(request, CancellationToken.None);
2224

2325
//Assert
2426
result.EntityId.Should().NotBeNull();
@@ -29,6 +31,8 @@ public async Task DemoThing_is_added()
2931

3032
protected override void TestClassFixtureSetup()
3133
{
32-
createDemoThingUseCase = new CreateDemoThingUseCase(DemoThingRepository, RequestDispatcher, Logger, CurrentUser);
34+
var logger = Substitute.For<ILogger<CreateDemoThingUseCase>>();
35+
36+
createDemoThingUseCase = new CreateDemoThingUseCase(DemoThingRepository, RequestDispatcher, logger, CurrentUser);
3337
}
3438
}

0 commit comments

Comments
 (0)