Skip to content

Commit 1d84877

Browse files
author
TheTribe
committed
Merge pull request #94 from jbatte47/dev
Issues #92 and #93 - fixed
2 parents 4675e7a + 6602438 commit 1d84877

9 files changed

Lines changed: 109 additions & 33 deletions

File tree

src/Patterns.Autofac/Logging/LoggingModule.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ protected override void Load(ContainerBuilder builder)
6868
{
6969
try
7070
{
71-
var configSource = context.Resolve<IConfigurationSource>();
72-
return configSource.GetSection<LoggingConfig>(LoggingConfig.SectionName);
71+
var configSource = context.ResolveOptional<IConfigurationSource>();
72+
var config = configSource != null ? configSource.GetSection<LoggingConfig>(LoggingConfig.SectionName) : null;
73+
return config ?? new LoggingConfig();
7374
}
7475
catch (ComponentNotRegisteredException registrationError)
7576
{
7677
throw ErrorBuilder.BuildContainerException(registrationError, ConfigurationResources.MissingConfigSourceErrorHint);
7778
}
78-
});
79+
}).As<ILoggingConfig>();
7980
builder.RegisterType<LoggingInterceptor>();
8081
}
8182

src/Patterns.Testing.Autofac/Moq/MoqRegistrationSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
5757
_log.Info(format => format(Resources.MoqRegistrationSource_RegistrationsFor_InfoFormat, service.Description));
5858

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

6262
var typedService = service as TypedService;
6363
bool canMock = typedService != null && (typedService.ServiceType.IsInterface || typedService.ServiceType.IsAbstract || !typedService.ServiceType.IsSealed);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#region FreeBSD
2+
3+
// Copyright (c) 2013, John Batte
4+
// All rights reserved.
5+
//
6+
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7+
//
8+
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9+
//
10+
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
11+
// documentation and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
14+
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
15+
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16+
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
17+
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
18+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
20+
#endregion
21+
22+
namespace Patterns.Logging
23+
{
24+
/// <summary>
25+
/// Defines configuration options for the Patterns.Logging namespace.
26+
/// </summary>
27+
public interface ILoggingConfig
28+
{
29+
/// <summary>
30+
/// Gets or sets a value indicating whether the logging interceptor should trap exceptions
31+
/// (as opposed to allowing them to bubble up).
32+
/// </summary>
33+
/// <value>
34+
/// <c>true</c> if the logging interceptor should trap exceptions; otherwise, <c>false</c>.
35+
/// </value>
36+
bool TrapExceptions { get; set; }
37+
}
38+
}

src/Patterns/Logging/LoggingConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace Patterns.Logging
3030
/// <summary>
3131
/// Defines configuration options for the Patterns.Logging namespace.
3232
/// </summary>
33-
public class LoggingConfig : ConfigurationSection
33+
public class LoggingConfig : ConfigurationSection, ILoggingConfig
3434
{
3535
/// <summary>
3636
/// The default section name.

src/Patterns/Logging/LoggingInterceptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class LoggingInterceptor : IInterceptor
4242
private const string _argumentListFormat = "({0})";
4343
private const string _argumentListSeparator = ",";
4444
private const string _stringDisplayFormat = @"""{0}""";
45-
private readonly LoggingConfig _config;
45+
private readonly ILoggingConfig _config;
4646
private readonly Func<Type, ILog> _logFactory;
4747

4848
private static readonly FuncStrategies<Type, object, object> _displayStrategies
@@ -58,7 +58,7 @@ private static readonly FuncStrategies<Type, object, object> _displayStrategies
5858
/// </summary>
5959
/// <param name="config">The config.</param>
6060
/// <param name="logFactory">The log factory.</param>
61-
public LoggingInterceptor(LoggingConfig config, Func<Type, ILog> logFactory)
61+
public LoggingInterceptor(ILoggingConfig config, Func<Type, ILog> logFactory)
6262
{
6363
_config = config;
6464
_logFactory = logFactory;

src/Patterns/Patterns.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
<Compile Include="Configuration\IConfigurationSource.cs" />
7272
<Compile Include="ExceptionHandling\ExceptionState.cs" />
7373
<Compile Include="ExceptionHandling\Try.cs" />
74+
<Compile Include="Logging\ILoggingConfig.cs" />
7475
<Compile Include="Logging\LoggingConfig.cs" />
7576
<Compile Include="Logging\LoggingInterceptor.cs" />
7677
<Compile Include="Logging\LoggingResources.Designer.cs">

src/_specs/Features/Testing/Moq/Autofac/AutofacMoqContainer.feature

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,31 @@
55

66
Background:
77
Given I have an Autofac/Moq test container
8+
Then the Autofac/Moq test container should have 0 registrations for my test object
89

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

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

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

2630
When I create a mock of the object using the test container
2731
And I create an object using the test container
28-
Then the test container should have given me an object
32+
Then the Autofac/Moq test container should have 2 registrations for my test object
33+
And the test container should have given me an object
2934
And the test container should have given me a mock of the object
3035
And the object retrieved by the test container should be a mock-based type

src/_specs/Features/Testing/Moq/Autofac/AutofacMoqContainer.feature.cs

Lines changed: 32 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/_specs/Steps/Testing/Moq/Autofac/AutofacTestContainerSteps.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
using FluentAssertions;
1+
using System.Linq;
2+
3+
using Autofac.Core;
4+
5+
using FluentAssertions;
26

37
using Patterns.Specifications.Models.Mocking;
8+
using Patterns.Specifications.Models.Testing.Moq;
49
using Patterns.Testing.Autofac.Moq;
510

611
using TechTalk.SpecFlow;
@@ -22,5 +27,21 @@ public void AssertMoqContainerIsAutofac()
2227
{
2328
_moq.Container.Should().NotBeNull().And.BeOfType<AutofacMoqContainer>();
2429
}
30+
31+
[Then(@"the Autofac/Moq test container should have (.*) registration(?:s)? for my test object")]
32+
public void AssertRegistrationCount(int expectedRegistrations)
33+
{
34+
IComponentRegistry registry = _moq.Container.As<IAutofacMoqContainer>().ComponentRegistry;
35+
36+
int registrationCount = registry.Registrations
37+
.Count(registration => registration.Services.Any(RegistrationMatchesType<TestContainerTarget>));
38+
39+
registrationCount.Should().Be(expectedRegistrations);
40+
}
41+
42+
private static bool RegistrationMatchesType<TService>(Service service)
43+
{
44+
return service is TypedService && ((TypedService)service).ServiceType == typeof(TService);
45+
}
2546
}
2647
}

0 commit comments

Comments
 (0)