Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Commit 407e70a

Browse files
committed
(maint) added some unittests
1 parent dfe36db commit 407e70a

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

src/Spectre.Console.Registrars.SimpleInjector.Tests/RegistrarTests.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
using Shouldly;
1+
using System;
2+
3+
using Shouldly;
24

35
using Xunit;
46

57
namespace Spectre.Console.Registrars.SimpleInjector.Tests
68
{
79
public class RegistrarTests
810
{
11+
[Fact]
12+
public void Resolver_Resolving_null_Throws()
13+
{
14+
var fixture = new RegistrarFixture();
15+
var resolver = fixture.GetResolver();
16+
17+
Action action = () => resolver.Resolve(null);
18+
19+
action.ShouldThrow<ArgumentNullException>();
20+
}
21+
922
[Fact]
1023
public void Resolver_Should_Return_Registration_From_Container()
1124
{
@@ -56,6 +69,20 @@ public void Resolver_Should_Return_Lazy_From_Registrar()
5669
ReferenceEquals(expected, actual).ShouldBeTrue();
5770
}
5871

72+
[Fact]
73+
public void Resolver_Resolving_From_Multiple_Lazies_Returns_The_Last_Registration()
74+
{
75+
var fixture = new RegistrarFixture();
76+
fixture.GivenMultiRegistrationTypes(typeof(ISomeInterface));
77+
fixture.GivenOnRegistrar(r => r.RegisterLazy(typeof(ISomeInterface), () => new SomeDependency()));
78+
fixture.GivenOnRegistrar(r => r.RegisterLazy(typeof(ISomeInterface), () => new SomeOtherDependency()));
79+
80+
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
81+
82+
actual.ShouldNotBeNull();
83+
actual.ShouldBeOfType<SomeOtherDependency>();
84+
}
85+
5986
[Fact(Skip = "Doesn't work at the moment")]
6087
public void Resolver_Should_Not_Call_Lazy_Factory_If_Not_Needed()
6188
{
@@ -99,6 +126,20 @@ public void Resolver_Resolving_From_Multiple_Returns_The_Last_Registration()
99126
actual.ShouldBeOfType<SomeOtherDependency>();
100127
}
101128

129+
[Fact]
130+
public void Resolver_Resolving_From_Multiple_Instances_Returns_The_Last_Registration()
131+
{
132+
var fixture = new RegistrarFixture();
133+
fixture.GivenMultiRegistrationTypes(typeof(ISomeInterface));
134+
fixture.GivenOnRegistrar(r => r.RegisterInstance(typeof(ISomeInterface), new SomeDependency()));
135+
fixture.GivenOnRegistrar(r => r.RegisterInstance(typeof(ISomeInterface), new SomeOtherDependency()));
136+
137+
var actual = fixture.GetResolver().Resolve(typeof(ISomeInterface));
138+
139+
actual.ShouldNotBeNull();
140+
actual.ShouldBeOfType<SomeOtherDependency>();
141+
}
142+
102143
private interface ISomeInterface
103144
{
104145
}

src/Spectre.Console.Registrars.SimpleInjector/SimlpeInjectorRegistrar.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
using SimpleInjector;
44
using Spectre.Console.Cli;
55

6-
namespace Spectre.Console.Registrars.SimpleInjector
6+
// ReSharper disable once CheckNamespace
7+
namespace Spectre.Console
78
{
89
/// <summary>
910
/// Implements <see cref="ITypeRegistrar"/> using a SimpleInjector <see cref="Container"/>.
@@ -72,8 +73,8 @@ public void RegisterLazy(Type service, Func<object> factory)
7273
// todo: non of these code-paths are lazy!!
7374
if (multiRegistrationTypes.Contains(service))
7475
{
75-
// todo: this ignores the lifestyle, but seemingly Func<object> can not be used on collections.
76-
container.Collection.AppendInstance(service, factory());
76+
var registration = lifestyle.CreateRegistration(service, factory, container);
77+
container.Collection.Append(service, registration);
7778
return;
7879
}
7980

@@ -105,12 +106,11 @@ public object Resolve(Type type)
105106
throw new ArgumentNullException(nameof(type));
106107
}
107108

108-
if (multiRegistrationTypes.Contains(type))
109-
{
110-
return container.GetAllInstances(type).LastOrDefault();
111-
}
109+
var implementation = multiRegistrationTypes.Contains(type)
110+
? container.GetAllInstances(type).LastOrDefault()
111+
: container.GetInstance(type);
112112

113-
return container.GetInstance(type);
113+
return implementation;
114114
}
115115
}
116116
}

0 commit comments

Comments
 (0)