Last active
April 4, 2016 15:42
-
-
Save JohanLarsson/b7c7705a575c021e56ae to your computer and use it in GitHub Desktop.
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
[Test] | |
public void SomeTest() | |
{ | |
var ioc = new TestIoC(); | |
var orderService = ioc.Get<OrderService>(); | |
... | |
} |
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
public partial class TestIoC | |
{ | |
private readonly IKernel _kernel = new StandardKernel(Root.Modules); | |
private Mock<ILogger> _loggerMock; | |
public TestIoC() | |
{ | |
BindMock(_ => LoggerMock); | |
} | |
public Mock<ILogger> LoggerMock | |
{ | |
get | |
{ | |
return _loggerMock ?? (_loggerMock = new Mock<ILogger>()); | |
} | |
} | |
public T Get<T>() | |
{ | |
_hasResolved = true; | |
return _kernel.Get<T>(); | |
} | |
public void BindMock<T>(Func<IContext, Mock<T>> mock) where T : class | |
{ | |
EnsureSetup(); | |
_kernel.Rebind<T>() | |
.ToMethod(c => mock(c).Object) | |
.InSingletonScope(); | |
} | |
private void EnsureSetup() | |
{ | |
if (_hasResolved) | |
{ | |
throw new InvalidOperationException("All setup must be done before resolving"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment