Skip to content

Commit

Permalink
ShouldRegisterServiceDescriptor -> PASS
Browse files Browse the repository at this point in the history
  • Loading branch information
LBoullosa committed Jun 3, 2024
1 parent 34ae0f1 commit cac47fb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions STX.SPAL.Core.Tests.Unit/STX.SPAL.Core.Tests.Unit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="CleanMoq" Version="1.0.0" />
<PackageReference Include="CompareNETObjects" Version="4.83.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Tynamix.ObjectFiller" Version="1.5.8" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Moq;

namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.ServicesCollections
Expand All @@ -19,16 +20,23 @@ private void ShouldRegisterServiceDescriptor()

ServiceDescriptor randomServiceDescriptor = randomProperties.ServiceDescriptor;
ServiceDescriptor inputServiceDescriptor = randomServiceDescriptor;
ServiceDescriptor expectedServiceDescriptor = inputServiceDescriptor;

IServiceCollection randomServiceCollection = randomProperties.ServiceCollection;
IServiceCollection expectedServiceCollection = randomServiceCollection;
IServiceCollection returnedServiceCollection = randomServiceCollection;

expectedServiceCollection.Add(inputServiceDescriptor);

this.dependencyInjectionBroker
.Setup(broker =>
broker.RegisterServiceDescriptor(
It.Is<ServiceDescriptor>(actualServiceDescriptor =>
actualServiceDescriptor == inputServiceDescriptor)))
SameServiceDescriptorAs(
actualServiceDescriptor,
expectedServiceDescriptor)
.Compile()
.Invoke(actualServiceDescriptor))))
.Returns(returnedServiceCollection);

// when
Expand All @@ -45,7 +53,11 @@ private void ShouldRegisterServiceDescriptor()
broker =>
broker.RegisterServiceDescriptor(
It.Is<ServiceDescriptor>(actualServiceDescriptor =>
actualServiceDescriptor == inputServiceDescriptor)),
SameServiceDescriptorAs(
actualServiceDescriptor,
expectedServiceDescriptor)
.Compile()
.Invoke(actualServiceDescriptor))),
Times.Once);

this.dependencyInjectionBroker.VerifyNoOtherCalls();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// ----------------------------------------------------------------------------------

using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using KellermanSoftware.CompareNetObjects;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using STX.SPAL.Abstractions;
Expand All @@ -17,11 +19,13 @@ namespace STX.SPAL.Core.Tests.Unit.Services.Foundations.ServicesCollections
public partial class ServiceCollectionServiceTests
{
private readonly Mock<IDependencyInjectionBroker> dependencyInjectionBroker;
private readonly ICompareLogic compareLogic;
private readonly IServiceCollectionService serviceCollectionService;

public ServiceCollectionServiceTests()
{
this.dependencyInjectionBroker = new Mock<IDependencyInjectionBroker>();
this.compareLogic = new CompareLogic();

this.serviceCollectionService =
new ServiceCollectionService(dependencyInjectionBroker.Object);
Expand Down Expand Up @@ -108,6 +112,12 @@ private static dynamic CreateRandomProperties()
ServiceLifeTime = randomServiceLifeTime,

ServiceDescriptor =
new ServiceDescriptor(
randomSpalInterfaceType,
randomImplementationType,
randomServiceLifeTime),

ServiceDescriptorWithSpalId =
new ServiceDescriptor(
randomSpalInterfaceType,
randomSpalId,
Expand All @@ -117,5 +127,16 @@ private static dynamic CreateRandomProperties()
ServiceCollection = new ServiceCollection()
};
}

private Expression<Func<ServiceDescriptor, bool>> SameServiceDescriptorAs(
ServiceDescriptor actualServiceDescriptor,
ServiceDescriptor expectedServiceDescriptor)
{
return actualServiceDescriptor =>
this.compareLogic.Compare(
expectedServiceDescriptor,
actualServiceDescriptor)
.AreEqual;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public IServiceCollection RegisterServiceDescriptor(
Type implementationType,
ServiceLifetime serviceLifetime)
{
throw new System.NotImplementedException();
ServiceDescriptor serviceDescriptor =
new ServiceDescriptor(spalInterfaceType, implementationType, serviceLifetime);

IServiceCollection serviceCollection = dependencyInjectionBroker.RegisterServiceDescriptor(serviceDescriptor);
return serviceCollection;
}
}
}

0 comments on commit cac47fb

Please sign in to comment.