-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from MRCollective/keyed-registrations
Keyed registration support
- Loading branch information
Showing
4 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
AutofacContrib.NSubstitute.Tests/KeyedRegistrationFixture.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Autofac.Features.Indexed; | ||
using NSubstitute; | ||
using NUnit.Framework; | ||
|
||
namespace AutofacContrib.NSubstitute.Tests | ||
{ | ||
public enum Switch | ||
{ | ||
Off, | ||
On | ||
} | ||
|
||
public class ClassWithKeyedDependencies | ||
{ | ||
public ClassWithKeyedDependencies(IIndex<Switch, IDependency2> dependencies) | ||
{ | ||
OnDependency = dependencies[Switch.On]; | ||
OffDependency = dependencies[Switch.Off]; | ||
} | ||
|
||
public IDependency2 OnDependency { get; private set; } | ||
public IDependency2 OffDependency { get; private set; } | ||
} | ||
|
||
public static class KeyedRegistrationFixture | ||
{ | ||
[Test] | ||
public static void ShouldResolveIndexedDependencies() | ||
{ | ||
var autoSubstitute = new AutoSubstitute(); | ||
|
||
var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>(); | ||
|
||
Assert.NotNull(target.OnDependency); | ||
Assert.NotNull(target.OffDependency); | ||
} | ||
|
||
[Test] | ||
public static void ShouldResolveASubstituteForIndexedDependency() | ||
{ | ||
var autoSubstitute = new AutoSubstitute(); | ||
var index = autoSubstitute.Resolve<IIndex<Switch, IDependency2>>(); | ||
index[Switch.On].SomeOtherMethod().Returns(5); | ||
|
||
var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>(); | ||
|
||
Assert.That(target.OnDependency.SomeOtherMethod(), Is.EqualTo(5)); | ||
} | ||
|
||
[Test] | ||
public static void ShouldAcceptProvidedIndexedDependency() | ||
{ | ||
var autoSubstitute = new AutoSubstitute(); | ||
var substitute = Substitute.For<IDependency2>(); | ||
substitute.SomeOtherMethod().Returns(5); | ||
autoSubstitute.Provide(substitute, Switch.On); | ||
|
||
var target = autoSubstitute.Resolve<ClassWithKeyedDependencies>(); | ||
|
||
Assert.That(target.OnDependency.SomeOtherMethod(), Is.EqualTo(5)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters