Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate lifecycles in UnitTests #13

Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading