forked from nblumhardt/autofac-serilog-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nblumhardt#34: adding tests for current behavior
- Loading branch information
1 parent
df1f578
commit 2794ad9
Showing
1 changed file
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using Autofac; | ||
using Moq; | ||
using Serilog; | ||
using Xunit; | ||
|
||
namespace AutofacSerilogIntegration.Tests | ||
{ | ||
public class ActivatorTests | ||
{ | ||
private readonly Mock<ILogger> _logger; | ||
|
||
public ActivatorTests() | ||
{ | ||
_logger = new Mock<ILogger>(); | ||
_logger.SetReturnsDefault(_logger.Object); | ||
} | ||
|
||
private void ResolveInstance<TDependency>(Action<ContainerBuilder> configureContainer) | ||
{ | ||
var containerBuilder = new ContainerBuilder(); | ||
containerBuilder.RegisterType<Component<TDependency>>(); | ||
containerBuilder.RegisterLogger(_logger.Object); | ||
configureContainer(containerBuilder); | ||
using (var container = containerBuilder.Build()) | ||
{ | ||
container.Resolve<Component<TDependency>>(); | ||
} | ||
} | ||
|
||
private void VerifyLoggerCreation<TContext>(Func<Times> times) | ||
=> _logger.Verify(logger => logger.ForContext(typeof(TContext)), times); | ||
|
||
[Fact] | ||
public void Default_ReflectionActivator_DependencyWithLogger_ShouldCreateLogger() | ||
{ | ||
ResolveInstance<DependencyWithLogger>(containerBuilder => containerBuilder.RegisterType<DependencyWithLogger>()); | ||
VerifyLoggerCreation<DependencyWithLogger>(Times.AtLeastOnce); | ||
} | ||
|
||
[Fact] | ||
public void Default_ReflectionActivator_DependencyWithoutLogger_ShouldNotCreateLogger() | ||
{ | ||
ResolveInstance<DependencyWithoutLogger>(containerBuilder => containerBuilder.RegisterType<DependencyWithoutLogger>()); | ||
VerifyLoggerCreation<DependencyWithoutLogger>(Times.Never); | ||
} | ||
|
||
[Fact] | ||
//legacy behavior | ||
public void Default_ProvidedInstanceActivator_DependencyWithoutLogger_CreatesLogger() | ||
{ | ||
ResolveInstance<DependencyWithoutLogger>(containerBuilder => containerBuilder.RegisterInstance(new DependencyWithoutLogger())); | ||
VerifyLoggerCreation<DependencyWithoutLogger>(Times.AtLeastOnce); | ||
} | ||
|
||
[Fact] | ||
//legacy behavior | ||
public void Default_DelegateActivator_DependencyWithoutLogger_CreatesLogger() | ||
{ | ||
ResolveInstance<DependencyWithoutLogger>(containerBuilder => containerBuilder.Register(_ => new DependencyWithoutLogger())); | ||
VerifyLoggerCreation<DependencyWithoutLogger>(Times.AtLeastOnce); | ||
} | ||
|
||
private class Component<TDependency> | ||
{ | ||
public Component(TDependency dependency) | ||
{ | ||
} | ||
} | ||
|
||
private class DependencyWithLogger | ||
{ | ||
public DependencyWithLogger(ILogger logger) | ||
{ | ||
} | ||
} | ||
|
||
private class DependencyWithoutLogger {} | ||
} | ||
} |