From 1da4503b050b11d80084d5181cb21eb6071d85ac Mon Sep 17 00:00:00 2001 From: Craig Fowler Date: Tue, 17 Sep 2024 20:18:14 +0100 Subject: [PATCH] Add further test coverage Also remove a condition which can never be exercised. ServiceDescriptor's own constructor makes this impossible. --- .../ServiceCollectionAdapter.cs | 4 +- .../ServiceCollectionAdapterTests.cs | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 Tests/CSF.Screenplay.SpecFlow.Tests/ServiceCollectionAdapterTests.cs diff --git a/CSF.Screenplay.SpecFlow/ServiceCollectionAdapter.cs b/CSF.Screenplay.SpecFlow/ServiceCollectionAdapter.cs index 1f28a148..6f1896ee 100644 --- a/CSF.Screenplay.SpecFlow/ServiceCollectionAdapter.cs +++ b/CSF.Screenplay.SpecFlow/ServiceCollectionAdapter.cs @@ -84,10 +84,8 @@ public void Add(ServiceDescriptor item) OpenGenericRegisterFactory.MakeGenericMethod(item.ServiceType).Invoke(this, new[] { item }); else if (item.ImplementationInstance != null) OpenGenericRegisterInstance.MakeGenericMethod(item.ServiceType).Invoke(this, new[] { item }); - else if (item.ImplementationType != null) - OpenGenericRegisterType.MakeGenericMethod(item.ServiceType, item.ImplementationType).Invoke(this, Array.Empty()); else - throw new ArgumentException($"Unsupported {nameof(ServiceDescriptor)}; one of implementation factory, instance or type must not be null", nameof(item)); + OpenGenericRegisterType.MakeGenericMethod(item.ServiceType, item.ImplementationType).Invoke(this, Array.Empty()); } void RegisterType() where TImpl : class,TSvc diff --git a/Tests/CSF.Screenplay.SpecFlow.Tests/ServiceCollectionAdapterTests.cs b/Tests/CSF.Screenplay.SpecFlow.Tests/ServiceCollectionAdapterTests.cs new file mode 100644 index 00000000..abf78ef1 --- /dev/null +++ b/Tests/CSF.Screenplay.SpecFlow.Tests/ServiceCollectionAdapterTests.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections; +using BoDi; +using Microsoft.Extensions.DependencyInjection; + +namespace CSF.Screenplay; + +[TestFixture,Parallelizable] +public class ServiceCollectionAdapterTests +{ + [Test] + public void UnsupportedFunctionalityShouldThrowNotSupportedException() + { + var container = new ObjectContainer(); + var sut = new ServiceCollectionAdapter(container); + + Assert.Multiple(() => + { + Assert.That(() => sut[0], Throws.InstanceOf(), "Indexer get"); + Assert.That(() => sut[0] = null, Throws.InstanceOf(), "Indexer set"); + Assert.That(() => sut.Clear(), Throws.InstanceOf(), nameof(IServiceCollection.Clear)); + Assert.That(() => sut.Contains(null), Throws.InstanceOf(), nameof(IServiceCollection.Contains)); + Assert.That(() => sut.CopyTo(null, default), Throws.InstanceOf(), nameof(IServiceCollection.CopyTo)); + Assert.That(() => sut.GetEnumerator(), Throws.InstanceOf(), nameof(IServiceCollection.GetEnumerator)); + Assert.That(() => ((IEnumerable) sut).GetEnumerator(), Throws.InstanceOf(), nameof(IServiceCollection.GetEnumerator) + ": " + nameof(IEnumerable)); + Assert.That(() => sut.IndexOf(default), Throws.InstanceOf(), nameof(IServiceCollection.IndexOf)); + Assert.That(() => sut.Insert(default, null), Throws.InstanceOf(), nameof(IServiceCollection.Insert)); + Assert.That(() => sut.Remove(null), Throws.InstanceOf(), nameof(IServiceCollection.Remove)); + Assert.That(() => sut.RemoveAt(default), Throws.InstanceOf(), nameof(IServiceCollection.RemoveAt)); + }); + } + + [Test] + public void AddTypeShouldAddToObjectContainer() + { + var container = new ObjectContainer(); + var sut = new ServiceCollectionAdapter(container); + + sut.AddSingleton(); + Assert.That(container.Resolve, Is.InstanceOf()); + } + + [Test] + public void AddInstanceShouldAddToObjectContainer() + { + var container = new ObjectContainer(); + var sut = new ServiceCollectionAdapter(container); + + var instance = new SampleType(); + sut.AddSingleton(instance); + Assert.That(container.Resolve(), Is.SameAs(instance)); + } + + [Test] + public void AddFactoryShouldAddToObjectContainer() + { + var container = new ObjectContainer(); + var sut = new ServiceCollectionAdapter(container); + + var instance = new SampleType(); + sut.AddSingleton(_ => instance); + Assert.That(container.Resolve(), Is.SameAs(instance)); + } + + interface ISampleInterface {} + + class SampleType : ISampleInterface {} +} \ No newline at end of file