Skip to content

Commit 82b0905

Browse files
committed
Merge branch 'feature/issue-68' into dev
2 parents fe2d892 + 144ece0 commit 82b0905

5 files changed

Lines changed: 220 additions & 3 deletions

File tree

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
using Autofac.Builder;
77
using Autofac.Core;
88

9+
using Common.Logging;
10+
911
using Moq;
1012

13+
using Patterns.Testing.Autofac.Properties;
14+
1115
namespace Patterns.Testing.Autofac.Moq
1216
{
1317
/// <summary>
1418
/// Provides a registration source for Autofac using Moq's MockRepository as a service factory.
1519
/// </summary>
1620
public class MoqRegistrationSource : IRegistrationSource
1721
{
22+
private static readonly ILog _log = LogManager.GetLogger(typeof (MoqRegistrationSource));
1823
private static readonly MockRepository _repository = new MockRepository(MockBehavior.Default);
1924
private static readonly MethodInfo _createMethod = typeof(MoqRegistrationSource)
2025
.GetMethod("CreateUsingRepository", BindingFlags.NonPublic | BindingFlags.Instance)
@@ -49,13 +54,18 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
4954
{
5055
if (service == null) throw new ArgumentNullException("service");
5156

57+
_log.Info(format => format(Resources.MoqRegistrationSource_RegistrationsFor_InfoFormat, service.Description));
58+
5259
IComponentRegistration[] existingRegistrations = registrationAccessor(service).ToArray();
5360
if (existingRegistrations.Length > 0) return existingRegistrations;
5461

5562
var typedService = service as TypedService;
56-
return typedService == null
57-
? Enumerable.Empty<IComponentRegistration>()
58-
: new[]
63+
bool canMock = typedService != null && (typedService.ServiceType.IsInterface || typedService.ServiceType.IsAbstract || !typedService.ServiceType.IsSealed);
64+
65+
if (canMock)
66+
{
67+
_log.Debug(format => format(Resources.MoqRegistrationSource_RegistrationsFor_PreMockCreateFormat, service.Description));
68+
return new[]
5969
{
6070
RegistrationBuilder
6171
.ForDelegate((context, parameters) =>
@@ -68,6 +78,9 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Fun
6878
.SingleInstance()
6979
.CreateRegistration()
7080
};
81+
}
82+
83+
return Enumerable.Empty<IComponentRegistration>();
7184
}
7285

7386
/// <summary>

src/Patterns.Testing.Autofac/Patterns.Testing.Autofac.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
<Reference Include="Autofac.Extras.CommonServiceLocator">
4545
<HintPath>..\packages\Autofac.Extras.CommonServiceLocator.3.0.0\lib\net40\Autofac.Extras.CommonServiceLocator.dll</HintPath>
4646
</Reference>
47+
<Reference Include="Common.Logging, Version=2.1.2.0, Culture=neutral, PublicKeyToken=af08829b84f0328e, processorArchitecture=MSIL">
48+
<SpecificVersion>False</SpecificVersion>
49+
<HintPath>..\packages\Common.Logging.2.1.2\lib\net40\Common.Logging.dll</HintPath>
50+
</Reference>
4751
<Reference Include="Microsoft.Practices.ServiceLocation">
4852
<HintPath>..\packages\CommonServiceLocator.1.0\lib\NET35\Microsoft.Practices.ServiceLocation.dll</HintPath>
4953
</Reference>
@@ -66,6 +70,11 @@
6670
<Compile Include="Moq\IAutofacMoqContainer.cs" />
6771
<Compile Include="Moq\MoqRegistrationSource.cs" />
6872
<Compile Include="Properties\AssemblyInfo.cs" />
73+
<Compile Include="Properties\Resources.Designer.cs">
74+
<AutoGen>True</AutoGen>
75+
<DesignTime>True</DesignTime>
76+
<DependentUpon>Resources.resx</DependentUpon>
77+
</Compile>
6978
</ItemGroup>
7079
<ItemGroup>
7180
<None Include="packages.config" />
@@ -92,6 +101,12 @@
92101
<ItemGroup>
93102
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
94103
</ItemGroup>
104+
<ItemGroup>
105+
<EmbeddedResource Include="Properties\Resources.resx">
106+
<Generator>ResXFileCodeGenerator</Generator>
107+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
108+
</EmbeddedResource>
109+
</ItemGroup>
95110
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
96111
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
97112
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

src/Patterns.Testing.Autofac/Properties/Resources.Designer.cs

Lines changed: 81 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<root>
3+
<!--
4+
Microsoft ResX Schema
5+
6+
Version 1.3
7+
8+
The primary goals of this format is to allow a simple XML format
9+
that is mostly human readable. The generation and parsing of the
10+
various data types are done through the TypeConverter classes
11+
associated with the data types.
12+
13+
Example:
14+
15+
... ado.net/XML headers & schema ...
16+
<resheader name="resmimetype">text/microsoft-resx</resheader>
17+
<resheader name="version">1.3</resheader>
18+
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
19+
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
20+
<data name="Name1">this is my long string</data>
21+
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
22+
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
23+
[base64 mime encoded serialized .NET Framework object]
24+
</data>
25+
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
26+
[base64 mime encoded string representing a byte array form of the .NET Framework object]
27+
</data>
28+
29+
There are any number of "resheader" rows that contain simple
30+
name/value pairs.
31+
32+
Each data row contains a name, and value. The row also contains a
33+
type or mimetype. Type corresponds to a .NET class that support
34+
text/value conversion through the TypeConverter architecture.
35+
Classes that don't support this are serialized and stored with the
36+
mimetype set.
37+
38+
The mimetype is used for serialized objects, and tells the
39+
ResXResourceReader how to depersist the object. This is currently not
40+
extensible. For a given mimetype the value must be set accordingly:
41+
42+
Note - application/x-microsoft.net.object.binary.base64 is the format
43+
that the ResXResourceWriter will generate, however the reader can
44+
read any of the formats listed below.
45+
46+
mimetype: application/x-microsoft.net.object.binary.base64
47+
value : The object must be serialized with
48+
: System.Serialization.Formatters.Binary.BinaryFormatter
49+
: and then encoded with base64 encoding.
50+
51+
mimetype: application/x-microsoft.net.object.soap.base64
52+
value : The object must be serialized with
53+
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
54+
: and then encoded with base64 encoding.
55+
56+
mimetype: application/x-microsoft.net.object.bytearray.base64
57+
value : The object must be serialized into a byte array
58+
: using a System.ComponentModel.TypeConverter
59+
: and then encoded with base64 encoding.
60+
-->
61+
62+
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
63+
<xsd:element name="root" msdata:IsDataSet="true">
64+
<xsd:complexType>
65+
<xsd:choice maxOccurs="unbounded">
66+
<xsd:element name="data">
67+
<xsd:complexType>
68+
<xsd:sequence>
69+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
70+
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
71+
</xsd:sequence>
72+
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
73+
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
74+
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
75+
</xsd:complexType>
76+
</xsd:element>
77+
<xsd:element name="resheader">
78+
<xsd:complexType>
79+
<xsd:sequence>
80+
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
81+
</xsd:sequence>
82+
<xsd:attribute name="name" type="xsd:string" use="required" />
83+
</xsd:complexType>
84+
</xsd:element>
85+
</xsd:choice>
86+
</xsd:complexType>
87+
</xsd:element>
88+
</xsd:schema>
89+
<resheader name="resmimetype">
90+
<value>text/microsoft-resx</value>
91+
</resheader>
92+
<resheader name="version">
93+
<value>1.3</value>
94+
</resheader>
95+
<resheader name="reader">
96+
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
97+
</resheader>
98+
<resheader name="writer">
99+
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
100+
</resheader>
101+
<data name="MoqRegistrationSource_RegistrationsFor_InfoFormat" xml:space="preserve">
102+
<value>finding registrations for {0}</value>
103+
</data>
104+
<data name="MoqRegistrationSource_RegistrationsFor_PreMockCreateFormat" xml:space="preserve">
105+
<value>no existing registrations found for {0}; creating with MockRepository</value>
106+
</data>
107+
</root>

src/Patterns.Testing.Autofac/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<packages>
33
<package id="Autofac" version="3.0.1" targetFramework="net45" />
44
<package id="Autofac.Extras.CommonServiceLocator" version="3.0.0" targetFramework="net45" />
5+
<package id="Common.Logging" version="2.1.2" targetFramework="net40" />
56
<package id="CommonServiceLocator" version="1.0" targetFramework="net45" />
67
<package id="Moq" version="4.0.10827" targetFramework="net45" />
78
</packages>

0 commit comments

Comments
 (0)