Skip to content

Commit

Permalink
[DependencyRegistrar] Remove exception when duplicate registering ser…
Browse files Browse the repository at this point in the history
…vice. Refactor tests.
  • Loading branch information
Aleksbgbg committed Jun 24, 2019
1 parent 58d7a41 commit 7422a5b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 43 deletions.
69 changes: 50 additions & 19 deletions Wingman.Tests/Container/DependencyRegistrarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

public class DependencyRegistrarTests
{
private const string ServiceKey = "Key";
private const string DefaultServiceKey = "Key";

private static readonly ServiceEntry DefaultServiceEntry = new ServiceEntry(typeof(IService), DefaultServiceKey);

private readonly Mock<IServiceEntryStore> _serviceEntryStoreMock;

Expand All @@ -40,7 +42,7 @@ public void TestRegisterInstance()
IService service = new Service();
SetupCreateInstance(service);

_dependencyRegistrar.RegisterInstance(typeof(IService), service, ServiceKey);
RegisterInstance(service);

VerifyRegisterStrategy();
}
Expand All @@ -50,7 +52,7 @@ public void TestRegisterSingleton()
{
SetupCreateSingleton();

_dependencyRegistrar.RegisterSingleton(typeof(IService), typeof(Service), ServiceKey);
RegisterSingleton();

VerifyRegisterStrategy();
}
Expand All @@ -60,7 +62,7 @@ public void TestRegisterPerRequest()
{
SetupCreatePerRequest();

_dependencyRegistrar.RegisterPerRequest(typeof(IService), typeof(Service), ServiceKey);
RegisterPerRequest();

VerifyRegisterStrategy();
}
Expand All @@ -71,15 +73,15 @@ public void TestRegisterHandler()
Func<IDependencyRetriever, object> handler = _ => new object();
SetupRegisterHandler(handler);

_dependencyRegistrar.RegisterHandler(typeof(IService), handler, ServiceKey);
RegisterHandler(handler);

VerifyRegisterStrategy();
}

[Fact]
public void TestUnregisterHandler()
{
_dependencyRegistrar.UnregisterHandler(typeof(IService), ServiceKey);
UnregisterHandler();

VerifyUnregisterHandler();
}
Expand All @@ -89,30 +91,26 @@ public void TestHasHandlerTrue()
{
SetupHasHandler(true);

bool hasHandler = _dependencyRegistrar.HasHandler(typeof(IService), ServiceKey);

Assert.True(hasHandler);
Assert.True(HasHandler());
}

[Fact]
public void TestHasHandlerFalse()
{
SetupHasHandler(false);

bool hasHandler = _dependencyRegistrar.HasHandler(typeof(IService), ServiceKey);

Assert.False(hasHandler);
Assert.False(HasHandler());
}

[Fact]
public void TestDuplicateRegistrationThrows()
public void TestDuplicateRegistrationDoesNotThrow()
{
SetupHasHandler(true);

Assert.Throws<InvalidOperationException>(() => _dependencyRegistrar.RegisterInstance(typeof(IService), null, ServiceKey));
Assert.Throws<InvalidOperationException>(() => _dependencyRegistrar.RegisterSingleton(typeof(IService), typeof(Service), ServiceKey));
Assert.Throws<InvalidOperationException>(() => _dependencyRegistrar.RegisterPerRequest(typeof(IService), typeof(Service), ServiceKey));
Assert.Throws<InvalidOperationException>(() => _dependencyRegistrar.RegisterHandler(typeof(IService), _ => new object(), ServiceKey));
RegisterInstance(null);
RegisterSingleton();
RegisterPerRequest();
RegisterHandler();
}

private void SetupCreateInstance(IService service)
Expand Down Expand Up @@ -147,6 +145,41 @@ private void SetupHasHandler(bool expectedResult)
.Returns(expectedResult);
}

private void RegisterInstance(IService implementation)
{
_dependencyRegistrar.RegisterInstance(typeof(IService), implementation, DefaultServiceKey);
}

private void RegisterSingleton()
{
_dependencyRegistrar.RegisterSingleton(typeof(IService), typeof(Service), DefaultServiceKey);
}

private void RegisterPerRequest()
{
_dependencyRegistrar.RegisterPerRequest(typeof(IService), typeof(Service), DefaultServiceKey);
}

private void RegisterHandler()
{
RegisterHandler(_ => new object());
}

private void RegisterHandler(Func<IDependencyRetriever, object> handler)
{
_dependencyRegistrar.RegisterHandler(typeof(IService), handler, DefaultServiceKey);
}

private bool HasHandler()
{
return _dependencyRegistrar.HasHandler(typeof(IService), DefaultServiceKey);
}

private void UnregisterHandler()
{
_dependencyRegistrar.UnregisterHandler(typeof(IService), DefaultServiceKey);
}

private void VerifyRegisterStrategy()
{
_serviceEntryStoreMock.Verify(store => store.InsertHandler(DefaultServiceEntry, _locationStrategy));
Expand All @@ -157,8 +190,6 @@ private void VerifyUnregisterHandler()
_serviceEntryStoreMock.Verify(store => store.RemoveHandler(DefaultServiceEntry));
}

private static ServiceEntry DefaultServiceEntry { get; } = new ServiceEntry(typeof(IService), ServiceKey);

private interface IService { }

private class Service : IService { }
Expand Down
6 changes: 0 additions & 6 deletions Wingman/Container/DependencyRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@ public override bool HasHandler(Type service, string key = null)
private void InsertHandler(Type service, string key, IServiceLocationStrategy serviceLocationStrategy)
{
ServiceEntry serviceEntry = MakeServiceEntry(service, key);

if (_serviceEntryStore.HasHandler(serviceEntry))
{
ThrowHelper.Throw.DependencyRegistrar.DuplicateRegistration(service);
}

_serviceEntryStore.InsertHandler(serviceEntry, serviceLocationStrategy);
}

Expand Down
18 changes: 0 additions & 18 deletions Wingman/Utilities/ThrowHelper.DependencyRegistrar.cs

This file was deleted.

0 comments on commit 7422a5b

Please sign in to comment.