Skip to content

Commit

Permalink
Merge pull request #13 from Wannesrebry/feature/validate-lifecycle-in…
Browse files Browse the repository at this point in the history
…-tests

Validate lifecycles in UnitTests
  • Loading branch information
Tim-Maes committed Oct 18, 2023
2 parents 299fedb + 04d4615 commit 4e85a3a
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 39 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,8 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

# JetBrains Rider
.idea/
*.sln.iml
4 changes: 4 additions & 0 deletions src/Bindicate.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
global using System.Linq;
global using System.Reflection;
global using Xunit;
global using FluentAssertions;
29 changes: 16 additions & 13 deletions src/Bindicate.Tests/Scoped/AddScopedAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Bindicate.Attributes;
using Bindicate.Configuration;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using Xunit;

namespace Bindicate.Tests.ScopedTests;

Expand All @@ -14,31 +11,37 @@ public class AddScopedAttributeTests
[Fact]
public void AddScoped_WithInterface_RegistersCorrectly()
{
//Arrange
var services = new ServiceCollection();
services.AddAutowiringForAssembly(_testAssembly);
var serviceProvider = services.BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
var service = scope.ServiceProvider.GetService<IScopedInterface>();
//Act
using var scope = serviceProvider.CreateScope();
var service = scope.ServiceProvider.GetService<IScopedInterface>();
ServiceDescriptor serviceDescriptor = services.First(x => x.ServiceType == typeof(IScopedInterface));

service.Should().NotBeNull().And.BeOfType<ScopedWithInterface>();
}
// Assert
service.Should().NotBeNull().And.BeOfType<ScopedWithInterface>();
serviceDescriptor.Lifetime.Should().Be(ServiceLifetime.Scoped);
}

[Fact]
public void AddScoped_RegistersCorrectly()
{
// Arrange
var services = new ServiceCollection();
services.AddAutowiringForAssembly(_testAssembly);
var serviceProvider = services.BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
var service = scope.ServiceProvider.GetService<SimpleScopedClass>();
// Act
using var scope = serviceProvider.CreateScope();
var service = scope.ServiceProvider.GetService<SimpleScopedClass>();
ServiceDescriptor serviceDescriptor = services.First(x => x.ServiceType == typeof(SimpleScopedClass));

service.Should().NotBeNull().And.BeOfType<SimpleScopedClass>();
}
// Assert
service.Should().NotBeNull().And.BeOfType<SimpleScopedClass>();
serviceDescriptor.Lifetime.Should().Be(ServiceLifetime.Scoped);
}
}

Expand Down
31 changes: 17 additions & 14 deletions src/Bindicate.Tests/Singleton/AddSingletonAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using Bindicate.Attributes;
using Bindicate.Configuration;
using Bindicate.Tests.ScopedTests;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using Xunit;

namespace Bindicate.Tests.Singleton;

Expand All @@ -28,31 +25,37 @@ public void AddSingleton_AlwaysReturnsSame()
[Fact]
public void AddSingleton_WithInterface_RegistersCorrectly()
{
// Arrange
var services = new ServiceCollection();
services.AddAutowiringForAssembly(_testAssembly);
var serviceProvider = services.BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
var service = scope.ServiceProvider.GetService<ISingletonInterface>();
// Act
using var scope = serviceProvider.CreateScope();
var service = scope.ServiceProvider.GetService<ISingletonInterface>();
ServiceDescriptor serviceDescriptor = services.First(x => x.ServiceType == typeof(ISingletonInterface));

service.Should().NotBeNull().And.BeOfType<SingletonWithInterface>();
}
// Assert
service.Should().NotBeNull().And.BeOfType<SingletonWithInterface>();
serviceDescriptor.Lifetime.Should().Be(ServiceLifetime.Singleton);
}

[Fact]
public void AddSingleton_RegistersCorrectly()
{
// Arrange
var services = new ServiceCollection();
services.AddAutowiringForAssembly(_testAssembly);
var serviceProvider = services.BuildServiceProvider();

// Act
using var scope = serviceProvider.CreateScope();
var service = scope.ServiceProvider.GetService<SimpleSingletonClass>();
ServiceDescriptor serviceDescriptor = services.First(x => x.ServiceType == typeof(SimpleSingletonClass));

using (var scope = serviceProvider.CreateScope())
{
var service = scope.ServiceProvider.GetService<SimpleSingletonClass>();

service.Should().NotBeNull().And.BeOfType<SimpleSingletonClass>();
}
// Assert
service.Should().NotBeNull().And.BeOfType<SimpleSingletonClass>();
serviceDescriptor.Lifetime.Should().Be(ServiceLifetime.Singleton);
}
}

Expand Down
28 changes: 17 additions & 11 deletions src/Bindicate.Tests/Transient/AddTransientAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,37 @@ public void AddTransient_AlwaysReturnsNewInstance()
[Fact]
public void AddTransient_WithInterface_RegistersCorrectly()
{
// Arrange
var services = new ServiceCollection();
services.AddAutowiringForAssembly(_testAssembly);
var serviceProvider = services.BuildServiceProvider();

// Act
using var scope = serviceProvider.CreateScope();
var service = scope.ServiceProvider.GetService<ITransientInterface>();
ServiceDescriptor serviceDescriptor = services.First(x => x.ServiceType == typeof(ITransientInterface));

using (var scope = serviceProvider.CreateScope())
{
var service = scope.ServiceProvider.GetService<ITransientInterface>();

service.Should().NotBeNull().And.BeOfType<TransientWithInterface>();
}
// Assert
service.Should().NotBeNull().And.BeOfType<TransientWithInterface>();
serviceDescriptor.Lifetime.Should().Be(ServiceLifetime.Transient);
}

[Fact]
public void AddSingleton_RegistersCorrectly()
{
// Arrange
var services = new ServiceCollection();
services.AddAutowiringForAssembly(_testAssembly);
var serviceProvider = services.BuildServiceProvider();

using (var scope = serviceProvider.CreateScope())
{
var service = scope.ServiceProvider.GetService<SimpleTransientClass>();
// Act
using var scope = serviceProvider.CreateScope();
var service = scope.ServiceProvider.GetService<SimpleTransientClass>();
ServiceDescriptor serviceDescriptor = services.First(x => x.ServiceType == typeof(SimpleTransientClass));

service.Should().NotBeNull().And.BeOfType<SimpleTransientClass>();
}
// Assert
service.Should().NotBeNull().And.BeOfType<SimpleTransientClass>();
serviceDescriptor.Lifetime.Should().Be(ServiceLifetime.Transient);
}
}

Expand Down

0 comments on commit 4e85a3a

Please sign in to comment.