Skip to content

Commit

Permalink
Merge pull request #94 from jbatte47/dev
Browse files Browse the repository at this point in the history
Issues #92 and #93 - fixed
  • Loading branch information
TheTribe committed Aug 21, 2013
2 parents 4675e7a + 6602438 commit 1d84877
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 33 deletions.
7 changes: 4 additions & 3 deletions src/Patterns.Autofac/Logging/LoggingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,15 @@ protected override void Load(ContainerBuilder builder)
{
try
{
var configSource = context.Resolve<IConfigurationSource>();
return configSource.GetSection<LoggingConfig>(LoggingConfig.SectionName);
var configSource = context.ResolveOptional<IConfigurationSource>();
var config = configSource != null ? configSource.GetSection<LoggingConfig>(LoggingConfig.SectionName) : null;
return config ?? new LoggingConfig();
}
catch (ComponentNotRegisteredException registrationError)
{
throw ErrorBuilder.BuildContainerException(registrationError, ConfigurationResources.MissingConfigSourceErrorHint);
}
});
}).As<ILoggingConfig>();
builder.RegisterType<LoggingInterceptor>();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Patterns.Testing.Autofac/Moq/MoqRegistrationSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
_log.Info(format => format(Resources.MoqRegistrationSource_RegistrationsFor_InfoFormat, service.Description));

IComponentRegistration[] existingRegistrations = registrationAccessor(service).ToArray();
if (existingRegistrations.Length > 0) return existingRegistrations;
if (existingRegistrations.Length > 0) return Enumerable.Empty<IComponentRegistration>();

var typedService = service as TypedService;
bool canMock = typedService != null && (typedService.ServiceType.IsInterface || typedService.ServiceType.IsAbstract || !typedService.ServiceType.IsSealed);
Expand Down
38 changes: 38 additions & 0 deletions src/Patterns/Logging/ILoggingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#region FreeBSD

// Copyright (c) 2013, John Batte
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#endregion

namespace Patterns.Logging
{
/// <summary>
/// Defines configuration options for the Patterns.Logging namespace.
/// </summary>
public interface ILoggingConfig
{
/// <summary>
/// Gets or sets a value indicating whether the logging interceptor should trap exceptions
/// (as opposed to allowing them to bubble up).
/// </summary>
/// <value>
/// <c>true</c> if the logging interceptor should trap exceptions; otherwise, <c>false</c>.
/// </value>
bool TrapExceptions { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Patterns/Logging/LoggingConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Patterns.Logging
/// <summary>
/// Defines configuration options for the Patterns.Logging namespace.
/// </summary>
public class LoggingConfig : ConfigurationSection
public class LoggingConfig : ConfigurationSection, ILoggingConfig
{
/// <summary>
/// The default section name.
Expand Down
4 changes: 2 additions & 2 deletions src/Patterns/Logging/LoggingInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class LoggingInterceptor : IInterceptor
private const string _argumentListFormat = "({0})";
private const string _argumentListSeparator = ",";
private const string _stringDisplayFormat = @"""{0}""";
private readonly LoggingConfig _config;
private readonly ILoggingConfig _config;
private readonly Func<Type, ILog> _logFactory;

private static readonly FuncStrategies<Type, object, object> _displayStrategies
Expand All @@ -58,7 +58,7 @@ public class LoggingInterceptor : IInterceptor
/// </summary>
/// <param name="config">The config.</param>
/// <param name="logFactory">The log factory.</param>
public LoggingInterceptor(LoggingConfig config, Func<Type, ILog> logFactory)
public LoggingInterceptor(ILoggingConfig config, Func<Type, ILog> logFactory)
{
_config = config;
_logFactory = logFactory;
Expand Down
1 change: 1 addition & 0 deletions src/Patterns/Patterns.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="Configuration\IConfigurationSource.cs" />
<Compile Include="ExceptionHandling\ExceptionState.cs" />
<Compile Include="ExceptionHandling\Try.cs" />
<Compile Include="Logging\ILoggingConfig.cs" />
<Compile Include="Logging\LoggingConfig.cs" />
<Compile Include="Logging\LoggingInterceptor.cs" />
<Compile Include="Logging\LoggingResources.Designer.cs">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,31 @@

Background:
Given I have an Autofac/Moq test container
Then the Autofac/Moq test container should have 0 registrations for my test object

Scenario: Create an unregistered object
When I create an object using the test container
Then the test container should have given me an object
And the Autofac/Moq test container should have 1 registration for my test object
And the object retrieved by the test container should be a mock-based type

Scenario: Create a registered object
When I register an object with the test container
And I create an object using the test container
Then the test container should have given me an object
Then the Autofac/Moq test container should have 1 registration for my test object
And the test container should have given me an object
And the object retrieved by the test container should not be a mock-based type

Scenario: Override a registered object
When I register an object with the test container
And I create an object using the test container
Then the test container should have given me an object
Then the Autofac/Moq test container should have 1 registration for my test object
And the test container should have given me an object
And the object retrieved by the test container should not be a mock-based type

When I create a mock of the object using the test container
And I create an object using the test container
Then the test container should have given me an object
Then the Autofac/Moq test container should have 2 registrations for my test object
And the test container should have given me an object
And the test container should have given me a mock of the object
And the object retrieved by the test container should be a mock-based type

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using FluentAssertions;
using System.Linq;

using Autofac.Core;

using FluentAssertions;

using Patterns.Specifications.Models.Mocking;
using Patterns.Specifications.Models.Testing.Moq;
using Patterns.Testing.Autofac.Moq;

using TechTalk.SpecFlow;
Expand All @@ -22,5 +27,21 @@ public void AssertMoqContainerIsAutofac()
{
_moq.Container.Should().NotBeNull().And.BeOfType<AutofacMoqContainer>();
}

[Then(@"the Autofac/Moq test container should have (.*) registration(?:s)? for my test object")]
public void AssertRegistrationCount(int expectedRegistrations)
{
IComponentRegistry registry = _moq.Container.As<IAutofacMoqContainer>().ComponentRegistry;

int registrationCount = registry.Registrations
.Count(registration => registration.Services.Any(RegistrationMatchesType<TestContainerTarget>));

registrationCount.Should().Be(expectedRegistrations);
}

private static bool RegistrationMatchesType<TService>(Service service)
{
return service is TypedService && ((TypedService)service).ServiceType == typeof(TService);
}
}
}

0 comments on commit 1d84877

Please sign in to comment.