Skip to content

Commit fe2d892

Browse files
committed
Merge branch 'feature/issue-67' into dev
2 parents cd5e9fb + 8ddc7f0 commit fe2d892

4 files changed

Lines changed: 89 additions & 10 deletions

File tree

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
namespace Patterns.Testing.Autofac.Moq
3535
{
3636
/// <summary>
37-
/// Provides a default implementation of the <see cref="IMoqContainer" /> interface.
37+
/// Provides a default implementation of the <see cref="IAutofacMoqContainer" /> interface.
3838
/// </summary>
3939
public sealed class AutofacMoqContainer : AccessibleContainer, IAutofacMoqContainer
4040
{
@@ -70,8 +70,13 @@ public AutofacMoqContainer() : this(new ContainerBuilder().Build()) {}
7070
/// </returns>
7171
public Mock<TService> Mock<TService>() where TService : class
7272
{
73-
var obj = (IMocked<TService>) Create<TService>();
74-
return obj.Mock;
73+
var service = Create<TService>();
74+
var existingMock = service as IMocked<TService>;
75+
if (existingMock != null) return existingMock.Mock;
76+
77+
var mock = MoqRegistrationSource.Repository.Create<TService>();
78+
Update(mock.Object);
79+
return mock;
7580
}
7681

7782
/// <summary>
@@ -85,13 +90,11 @@ public Mock<TService> Mock<TService>() where TService : class
8590
/// </returns>
8691
public TService Create<TService>(Func<IMoqContainer, TService> activator = null) where TService : class
8792
{
88-
Action<ContainerBuilder> defaultRegistration = builder => builder.RegisterType<TService>()
89-
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues);
90-
91-
Action<ContainerBuilder> activatorRegistration = builder => builder.Register(c => activator(this))
92-
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues);
93-
94-
return ResolveOrCreate<TService>(activator == null ? defaultRegistration : activatorRegistration);
93+
return ResolveOrCreate<TService>(activator == null
94+
? (builder => builder.RegisterType<TService>()
95+
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues))
96+
: (Action<ContainerBuilder>) (builder => builder.Register(c => activator(this))
97+
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues)));
9598
}
9699

97100
/// <summary>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ public class MoqRegistrationSource : IRegistrationSource
2020
.GetMethod("CreateUsingRepository", BindingFlags.NonPublic | BindingFlags.Instance)
2121
.GetGenericMethodDefinition();
2222

23+
/// <summary>
24+
/// Gets the repository.
25+
/// </summary>
26+
/// <value>
27+
/// The repository.
28+
/// </value>
29+
public static MockRepository Repository { get { return _repository; } }
30+
2331
/// <summary>
2432
/// Retrieve registrations for an unregistered service, to be used
2533
/// by the container.

src/Patterns/Patterns.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Compile Include="Reflection\PropertyValue.cs" />
8686
<Compile Include="Runtime\DefaultDateTimeInfo.cs" />
8787
<Compile Include="Runtime\IDateTimeInfo.cs" />
88+
<Compile Include="Runtime\TemporaryScope.cs" />
8889
<Compile Include="Runtime\TimeExtensions.cs" />
8990
<Compile Include="SolutionAssemblyInfo.cs">
9091
<AutoGen>True</AutoGen>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
using System;
23+
24+
namespace Patterns.Runtime
25+
{
26+
/// <summary>
27+
/// Provides a disposable scope with configurable setup and tear-down actions.
28+
/// </summary>
29+
public class TemporaryScope : IDisposable
30+
{
31+
private readonly Action _setup;
32+
private readonly Action _tearDown;
33+
34+
/// <summary>
35+
/// Initializes a new instance of the <see cref="TemporaryScope" /> class.
36+
/// </summary>
37+
/// <param name="setup">The setup.</param>
38+
/// <param name="tearDown">The tear down.</param>
39+
public TemporaryScope(Action setup = null, Action tearDown = null)
40+
{
41+
_setup = setup;
42+
_tearDown = tearDown;
43+
44+
if (_setup != null) _setup();
45+
}
46+
47+
/// <summary>
48+
/// Disposes this scope; if a tear-down method exists, it is executed.
49+
/// </summary>
50+
public void Dispose()
51+
{
52+
if(Disposed) throw new ObjectDisposedException(GetType().Name);
53+
54+
if (_tearDown != null) _tearDown();
55+
56+
Disposed = true;
57+
}
58+
59+
/// <summary>
60+
/// Gets or sets a value indicating whether this <see cref="TemporaryScope"/> is disposed.
61+
/// </summary>
62+
/// <value>
63+
/// <c>true</c> if disposed; otherwise, <c>false</c>.
64+
/// </value>
65+
public bool Disposed { get; set; }
66+
}
67+
}

0 commit comments

Comments
 (0)