Skip to content

Commit

Permalink
Add further test coverage
Browse files Browse the repository at this point in the history
Also remove a condition which can never be exercised.
ServiceDescriptor's own constructor makes this impossible.
  • Loading branch information
craigfowler committed Sep 17, 2024
1 parent c0e83c4 commit 1da4503
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
4 changes: 1 addition & 3 deletions CSF.Screenplay.SpecFlow/ServiceCollectionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>());
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<object>());
}

void RegisterType<TSvc,TImpl>() where TImpl : class,TSvc
Expand Down
Original file line number Diff line number Diff line change
@@ -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<NotSupportedException>(), "Indexer get");
Assert.That(() => sut[0] = null, Throws.InstanceOf<NotSupportedException>(), "Indexer set");
Assert.That(() => sut.Clear(), Throws.InstanceOf<NotSupportedException>(), nameof(IServiceCollection.Clear));
Assert.That(() => sut.Contains(null), Throws.InstanceOf<NotSupportedException>(), nameof(IServiceCollection.Contains));
Assert.That(() => sut.CopyTo(null, default), Throws.InstanceOf<NotSupportedException>(), nameof(IServiceCollection.CopyTo));
Assert.That(() => sut.GetEnumerator(), Throws.InstanceOf<NotSupportedException>(), nameof(IServiceCollection.GetEnumerator));
Assert.That(() => ((IEnumerable) sut).GetEnumerator(), Throws.InstanceOf<NotSupportedException>(), nameof(IServiceCollection.GetEnumerator) + ": " + nameof(IEnumerable));
Assert.That(() => sut.IndexOf(default), Throws.InstanceOf<NotSupportedException>(), nameof(IServiceCollection.IndexOf));
Assert.That(() => sut.Insert(default, null), Throws.InstanceOf<NotSupportedException>(), nameof(IServiceCollection.Insert));
Assert.That(() => sut.Remove(null), Throws.InstanceOf<NotSupportedException>(), nameof(IServiceCollection.Remove));
Assert.That(() => sut.RemoveAt(default), Throws.InstanceOf<NotSupportedException>(), nameof(IServiceCollection.RemoveAt));
});
}

[Test]
public void AddTypeShouldAddToObjectContainer()
{
var container = new ObjectContainer();
var sut = new ServiceCollectionAdapter(container);

sut.AddSingleton<ISampleInterface, SampleType>();
Assert.That(container.Resolve<ISampleInterface>, Is.InstanceOf<SampleType>());
}

[Test]
public void AddInstanceShouldAddToObjectContainer()
{
var container = new ObjectContainer();
var sut = new ServiceCollectionAdapter(container);

var instance = new SampleType();
sut.AddSingleton<ISampleInterface>(instance);
Assert.That(container.Resolve<ISampleInterface>(), Is.SameAs(instance));
}

[Test]
public void AddFactoryShouldAddToObjectContainer()
{
var container = new ObjectContainer();
var sut = new ServiceCollectionAdapter(container);

var instance = new SampleType();
sut.AddSingleton<ISampleInterface>(_ => instance);
Assert.That(container.Resolve<ISampleInterface>(), Is.SameAs(instance));
}

interface ISampleInterface {}

class SampleType : ISampleInterface {}
}

0 comments on commit 1da4503

Please sign in to comment.