Skip to content

Commit fc7f668

Browse files
author
TheTribe
committed
Merge pull request #118 from TheTribe/release/3.10-beta.4
Release/3.10-beta.4 to master
2 parents c3cd70a + 559db91 commit fc7f668

File tree

12 files changed

+146
-88
lines changed

12 files changed

+146
-88
lines changed

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The following log details the outward-facing changes made to code-patterns since its first migration to GitHub.
44

5+
## 3.10-beta.4 ##
6+
7+
- Fixes for `IMoqContainer` ([issue 114](https://github.com/TheTribe/code-patterns/issues/114))
8+
- Calls to `Create<TService>` now either return the registered instance, or a mock. No hidden updates.
9+
- Calls to `Create<TService, TImplementation>` update the container with the implementation type, and return the newly-registered instance.
10+
511
## 3.10-beta.3 ##
612

713
- Extracted `Patterns.Configuration.InMemoryConfigurationSource` from `Patterns.Testing.Configuration.TestConfigurationSource` ([issue 110](https://github.com/TheTribe/code-patterns/issues/110))

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

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ public AutofacMoqContainer() : this(new ContainerBuilder().Build()) {}
7171
/// </returns>
7272
public Mock<TService> Mock<TService>() where TService : class
7373
{
74-
var service = Try.Get(() => Create<TService>());
74+
TService service = Try.Get(Create<TService>);
7575
var existingMock = service as IMocked<TService>;
7676
if (existingMock != null) return existingMock.Mock;
7777

78-
var mock = MoqRegistrationSource.Repository.Create<TService>();
78+
Mock<TService> mock = MoqRegistrationSource.Repository.Create<TService>();
7979
Update(mock.Object);
8080
return mock;
8181
}
@@ -85,17 +85,25 @@ public Mock<TService> Mock<TService>() where TService : class
8585
/// for all unregistered dependencies.
8686
/// </summary>
8787
/// <typeparam name="TService">The type of the service.</typeparam>
88-
/// <param name="activator">The optional activator.</param>
8988
/// <returns>
9089
/// The service instance.
9190
/// </returns>
92-
public TService Create<TService>(Func<IMoqContainer, TService> activator = null) where TService : class
91+
public TService Create<TService>() where TService : class
9392
{
94-
return ResolveOrCreate<TService>(activator == null
95-
? (builder => builder.RegisterType<TService>()
96-
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues))
97-
: (Action<ContainerBuilder>) (builder => builder.Register(c => activator(this))
98-
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues)));
93+
return Container.Resolve<TService>();
94+
}
95+
96+
/// <summary>
97+
/// Creates an instance of the specified implementation (as the specified service),
98+
/// injecting mocked objects for all unregistered dependencies.
99+
/// </summary>
100+
/// <typeparam name="TService">The type of the service.</typeparam>
101+
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
102+
/// <returns></returns>
103+
public TService Create<TService, TImplementation>() where TService : class where TImplementation : TService
104+
{
105+
Update<TService, TImplementation>();
106+
return Create<TService>();
99107
}
100108

101109
/// <summary>
@@ -109,7 +117,7 @@ public TService Create<TService>(Func<IMoqContainer, TService> activator = null)
109117
public IMoqContainer Update<TService, TImplementation>() where TService : class where TImplementation : TService
110118
{
111119
UpdateWithBuilder(builder => builder.RegisterType<TImplementation>().As<TService>()
112-
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues));
120+
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues));
113121

114122
return this;
115123
}
@@ -125,7 +133,7 @@ public IMoqContainer Update<TService, TImplementation>() where TService : class
125133
public IMoqContainer Update<TService>(TService instance) where TService : class
126134
{
127135
UpdateWithBuilder(builder => builder.RegisterInstance(instance).As<TService>()
128-
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues));
136+
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues));
129137

130138
return this;
131139
}
@@ -141,17 +149,17 @@ public IMoqContainer Update<TService>(TService instance) where TService : class
141149
public IMoqContainer Update<TService>(Func<IMoqContainer, TService> activator) where TService : class
142150
{
143151
UpdateWithBuilder(builder => builder.Register(c => activator(this)).As<TService>()
144-
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues));
152+
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues));
145153

146154
return this;
147155
}
148156

149157
/// <summary>
150-
/// Updates the container using the specified module.
158+
/// Updates the container using the specified module.
151159
/// </summary>
152160
/// <param name="module">The module.</param>
153161
/// <returns>
154-
/// The container.
162+
/// The container.
155163
/// </returns>
156164
public IAutofacMoqContainer Update(Module module)
157165
{
@@ -160,11 +168,11 @@ public IAutofacMoqContainer Update(Module module)
160168
}
161169

162170
/// <summary>
163-
/// Updates the container using the specified registration.
171+
/// Updates the container using the specified registration.
164172
/// </summary>
165173
/// <param name="registration">The registration.</param>
166174
/// <returns>
167-
/// The container.
175+
/// The container.
168176
/// </returns>
169177
public IAutofacMoqContainer Update(Action<ContainerBuilder> registration)
170178
{
@@ -178,11 +186,5 @@ private void UpdateWithBuilder(Action<ContainerBuilder> registration)
178186
registration(builder);
179187
builder.Update(Container);
180188
}
181-
182-
private T ResolveOrCreate<T>(Action<ContainerBuilder> registration)
183-
{
184-
if (!Container.IsRegistered<T>()) UpdateWithBuilder(registration);
185-
return Container.Resolve<T>();
186-
}
187189
}
188190
}

src/Patterns.Testing/Moq/IMoqContainer.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,59 @@
2828
namespace Patterns.Testing.Moq
2929
{
3030
/// <summary>
31-
/// Provides an IoC container designed for maximum configurability
32-
/// during tests, and for tight integration with Moq.
31+
/// Provides an IoC container designed for maximum configurability
32+
/// during tests, and for tight integration with Moq.
3333
/// </summary>
3434
public interface IMoqContainer
3535
{
3636
/// <summary>
37-
/// Gets the locator.
37+
/// Gets the locator.
3838
/// </summary>
3939
/// <value>
40-
/// The locator.
40+
/// The locator.
4141
/// </value>
4242
IServiceLocator Locator { get; }
4343

4444
/// <summary>
45-
/// Retrieves the mock for the specified service type.
45+
/// Retrieves the mock for the specified service type.
4646
/// </summary>
4747
/// <typeparam name="TService">The type of the service.</typeparam>
48-
/// <returns>The service mock.</returns>
4948
Mock<TService> Mock<TService>() where TService : class;
5049

5150
/// <summary>
52-
/// Creates an instance of the specified service, injecting mocked objects
53-
/// for all unregistered dependencies.
51+
/// Creates an instance of the specified service, injecting mocked objects
52+
/// for all unregistered dependencies.
5453
/// </summary>
5554
/// <typeparam name="TService">The type of the service.</typeparam>
56-
/// <param name="activator">The optional activator.</param>
57-
/// <returns>The service instance.</returns>
58-
TService Create<TService>(Func<IMoqContainer, TService> activator = null) where TService : class;
55+
TService Create<TService>() where TService : class;
5956

6057
/// <summary>
61-
/// Updates this instance by registering the implementation type as the service type.
58+
/// Creates an instance of the specified implementation (as the specified service),
59+
/// injecting mocked objects for all unregistered dependencies.
60+
/// </summary>
61+
/// <typeparam name="TService">The type of the service.</typeparam>
62+
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
63+
TService Create<TService, TImplementation>() where TService : class where TImplementation : TService;
64+
65+
/// <summary>
66+
/// Updates this instance by registering the implementation type as the service type.
6267
/// </summary>
6368
/// <typeparam name="TService">The type of the service.</typeparam>
6469
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
65-
/// <returns>The container.</returns>
6670
IMoqContainer Update<TService, TImplementation>() where TService : class where TImplementation : TService;
6771

6872
/// <summary>
69-
/// Updates this instance by registering an instance of the specified service.
73+
/// Updates this instance by registering an instance of the specified service.
7074
/// </summary>
7175
/// <typeparam name="TService">The type of the service.</typeparam>
7276
/// <param name="instance">The instance.</param>
73-
/// <returns>The container.</returns>
7477
IMoqContainer Update<TService>(TService instance) where TService : class;
7578

7679
/// <summary>
77-
/// Updates this instance by registering the specified activator as the service type.
80+
/// Updates this instance by registering the specified activator as the service type.
7881
/// </summary>
7982
/// <typeparam name="TService">The type of the service.</typeparam>
8083
/// <param name="activator">The activator.</param>
81-
/// <returns>The container</returns>
8284
IMoqContainer Update<TService>(Func<IMoqContainer, TService> activator) where TService : class;
8385
}
8486
}

src/Patterns/SolutionAssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
[assembly: ComVisible(false)]
3333
[assembly: NeutralResourcesLanguage("en-US")]
3434
[assembly: AssemblyVersion("3.10.0")]
35-
[assembly: AssemblyInformationalVersion("3.10.0-beta3")]
35+
[assembly: AssemblyInformationalVersion("3.10.0-beta4")]

src/Patterns/SolutionAssemblyInfo.settings.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Settings.Initialize(
33
majorNumber: 3,
44
minorNumber: 10,
55
patchNumber: 0,
6-
buildNumber: 3,
6+
buildNumber: 4,
77
buildLevel: BuildLevels.Beta
88
);
99
#><#+

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,37 @@
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
8+
Then the Autofac/Moq test container should have 0 registrations for my test interface
99

10-
Scenario: Create an unregistered object
11-
When I create an object using the test container
10+
Scenario: Create an unregistered interface
11+
When I create an instance of an interface using the test container
1212
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
13+
And the Autofac/Moq test container should have 1 registration for my test interface
1414
And the object retrieved by the test container should be a mock-based type
1515

16+
Scenario: Create an unregistered creatable class
17+
When I create an instance of a creatable class using the test container
18+
Then the test container should have given me an object
19+
And the Autofac/Moq test container should have 1 registration for my test interface
20+
And the object retrieved by the test container should not be a mock-based type
21+
1622
Scenario: Create a registered object
1723
When I register an object with the test container
18-
And I create an object using the test container
19-
Then the Autofac/Moq test container should have 1 registration for my test object
24+
And I create an instance of an interface using the test container
25+
Then the Autofac/Moq test container should have 1 registration for my test interface
2026
And the test container should have given me an object
2127
And the object retrieved by the test container should not be a mock-based type
2228

2329
Scenario: Override a registered object
2430
When I register an object with the test container
25-
And I create an object using the test container
26-
Then the Autofac/Moq test container should have 1 registration for my test object
31+
And I create an instance of an interface using the test container
32+
Then the Autofac/Moq test container should have 1 registration for my test interface
2733
And the test container should have given me an object
2834
And the object retrieved by the test container should not be a mock-based type
2935

3036
When I create a mock of the object using the test container
31-
And I create an object using the test container
32-
Then the Autofac/Moq test container should have 2 registrations for my test object
37+
And I create an instance of an interface using the test container
38+
Then the Autofac/Moq test container should have 2 registrations for my test interface
3339
And the test container should have given me an object
3440
And the test container should have given me a mock of the object
3541
And the object retrieved by the test container should be a mock-based type

0 commit comments

Comments
 (0)