Skip to content

Commit fefd508

Browse files
committed
Merge pull request #18 from scott-xu/master
Support additional interfaces when mock
2 parents a38bfbb + c209ffc commit fefd508

File tree

13 files changed

+217
-22
lines changed

13 files changed

+217
-22
lines changed

ReleaseNotes.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Version 3.2.1.0
2+
---------------
3+
Added: Support additional interfaces when mock
4+
15
Version 3.0.0.0
26
---------------
37
Removed: No web builds. All builds are have not reference to web anymore

src/Ninject.MockingKernel.Moq.Test/MoqIntegrationTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ public MethodInfo CreateMethod
124124
throw new NotImplementedException();
125125
}
126126
}
127+
128+
public MethodInfo AddAdditionalInterfaceMethod
129+
{
130+
get
131+
{
132+
throw new NotImplementedException();
133+
}
134+
}
127135
}
128136
#endif
129137
}

src/Ninject.MockingKernel.Moq/DefaultMockRepositoryProvider.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public class DefaultMockRepositoryProvider : NinjectComponent, IMockRepositoryPr
4444
/// </summary>
4545
private MethodInfo createMethod;
4646

47+
/// <summary>
48+
/// Gets the method info of the add additional interface method.
49+
/// </summary>
50+
private MethodInfo addAdditionalInterfaceMethod;
51+
4752
/// <summary>
4853
/// Gets the instance of the mock repository.
4954
/// </summary>
@@ -77,6 +82,23 @@ public MethodInfo CreateMethod
7782
return this.createMethod;
7883
}
7984
}
85+
86+
/// <summary>
87+
/// Gets the method info of the add additional interface method.
88+
/// </summary>
89+
/// <value>The method info of the add additional interface method.</value>
90+
public MethodInfo AddAdditionalInterfaceMethod
91+
{
92+
get
93+
{
94+
if (this.addAdditionalInterfaceMethod == null)
95+
{
96+
this.addAdditionalInterfaceMethod = typeof(Mock).GetMethod("As");
97+
}
98+
99+
return this.addAdditionalInterfaceMethod;
100+
}
101+
}
80102
}
81103
}
82104
#endif

src/Ninject.MockingKernel.Moq/IMockRepositoryProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public interface IMockRepositoryProvider : INinjectComponent
4242
/// </summary>
4343
/// <value>The method info of the create method.</value>
4444
MethodInfo CreateMethod { get; }
45+
46+
/// <summary>
47+
/// Gets the method info of the add additional interface method.
48+
/// </summary>
49+
/// <value>the method info of the add additional interface method.</value>
50+
MethodInfo AddAdditionalInterfaceMethod { get; }
4551
}
4652
}
4753
#endif

src/Ninject.MockingKernel.Moq/MoqMockProvider.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace Ninject.MockingKernel.Moq
2323
{
2424
using System;
25+
using System.Linq;
2526
using System.Reflection;
2627

2728
using global::Moq;
@@ -45,6 +46,11 @@ public class MoqMockProvider : NinjectComponent, IProvider, IMockProviderCallbac
4546
/// </summary>
4647
private readonly MethodInfo createMethod;
4748

49+
/// <summary>
50+
/// The method info used to add additional interface to mock.
51+
/// </summary>
52+
private readonly MethodInfo addAdditionalInterfaceMethod;
53+
4854
/// <summary>
4955
/// Initializes a new instance of the <see cref="MoqMockProvider"/> class.
5056
/// </summary>
@@ -53,6 +59,7 @@ public MoqMockProvider(IMockRepositoryProvider mockRepositoryProvider)
5359
{
5460
this.mockRepository = mockRepositoryProvider.Instance;
5561
this.createMethod = mockRepositoryProvider.CreateMethod;
62+
this.addAdditionalInterfaceMethod = mockRepositoryProvider.AddAdditionalInterfaceMethod;
5663
}
5764
#endif
5865

@@ -87,6 +94,12 @@ public object Create(IContext context)
8794
{
8895
var methodInfo = this.createMethod.MakeGenericMethod(context.Request.Service);
8996
var mock = (Mock)methodInfo.Invoke(this.mockRepository, new object[0]);
97+
var additionalInterfaces = context.Parameters.OfType<AdditionalInterfaceParameter>().Select(ai => (Type)ai.GetValue(context, null));
98+
foreach (var additionalInterface in additionalInterfaces)
99+
{
100+
this.addAdditionalInterfaceMethod.MakeGenericMethod(additionalInterface).Invoke(mock, null);
101+
}
102+
90103
return mock.Object;
91104
}
92105

@@ -104,6 +117,12 @@ public object Create(IContext context)
104117
var mockType = typeof(Mock<>).MakeGenericType(context.Request.Service);
105118
var constructorInfo = mockType.GetConstructor(new[] { typeof(MockBehavior) });
106119
var mock = (Mock)constructorInfo.Invoke(new object[] { Settings.GetMockBehavior() });
120+
var additionalInterfaces = context.Parameters.OfType<AdditionalInterfaceParameter>().Select(ai => (Type)ai.GetValue(context, null));
121+
foreach (var additionalInterface in additionalInterfaces)
122+
{
123+
typeof(Mock).GetMethod("As").MakeGenericMethod(additionalInterface).Invoke(mock, null);
124+
}
125+
107126
return mock.Object;
108127
}
109128
#endif

src/Ninject.MockingKernel.NSubstitute/NSubstituteMockProvider.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace Ninject.MockingKernel.NSubstitute
2323
{
2424
using System;
25+
using System.Linq;
2526
using Activation;
2627
using Components;
2728
using global::NSubstitute;
@@ -61,7 +62,9 @@ public Func<IContext, IProvider> GetCreationCallback()
6162
/// </returns>
6263
public object Create(IContext context)
6364
{
64-
return Substitute.For(new[] { context.Request.Service }, null);
65+
var additionalInterfaces = context.Parameters.OfType<AdditionalInterfaceParameter>().Select(ai => (Type)ai.GetValue(context, null));
66+
67+
return Substitute.For(new[] { context.Request.Service }.Concat(additionalInterfaces).ToArray(), null);
6568
}
6669
}
6770
}

src/Ninject.MockingKernel.RhinoMock/RhinoMocksMockProvider.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,6 @@ namespace Ninject.MockingKernel.RhinoMock
3535
/// </summary>
3636
public class RhinoMocksMockProvider : NinjectComponent, IProvider, IMockProviderCallbackProvider
3737
{
38-
#if SILVERLIGHT
39-
/// <summary>
40-
/// The method info for creation mocks.
41-
/// </summary>
42-
private readonly MethodInfo generateMockMethodInfo;
43-
44-
/// <summary>
45-
/// Initializes a new instance of the <see cref="RhinoMocksMockProvider"/> class.
46-
/// </summary>
47-
public RhinoMocksMockProvider()
48-
{
49-
this.generateMockMethodInfo = typeof(MockRepository).GetMethod("GenerateMock");
50-
}
51-
#endif
52-
5338
/// <summary>
5439
/// Gets the type (or prototype) of instances the provider creates.
5540
/// </summary>
@@ -68,11 +53,9 @@ public Type Type
6853
/// <returns>The created instance.</returns>
6954
public object Create(IContext context)
7055
{
71-
#if !SILVERLIGHT
72-
return MockRepository.GenerateMock(context.Request.Service, new Type[0], new object[0]);
73-
#else
74-
return this.generateMockMethodInfo.MakeGenericMethod(context.Request.Service).Invoke(null, new[] { new object[0] });
75-
#endif
56+
var additionalInterfaces = context.Parameters.OfType<AdditionalInterfaceParameter>().Select(ai => (Type)ai.GetValue(context, null)).ToArray();
57+
58+
return MockRepository.GenerateMock(context.Request.Service, additionalInterfaces, new object[0]);
7659
}
7760

7861
/// <summary>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="IDummyService2.cs" company="bbv Software Services AG">
3+
// Copyright (c) 2010 bbv Software Services AG
4+
// Author: Remo Gloor [email protected]
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Also licenced under Microsoft Public License (Ms-PL).
19+
// </copyright>
20+
// --------------------------------------------------------------------------------------------------------------------
21+
22+
namespace Ninject.MockingKernel
23+
{
24+
/// <summary>
25+
/// A dummy interface
26+
/// </summary>
27+
public interface IDummyService2
28+
{
29+
/// <summary>
30+
/// A dummy method.
31+
/// </summary>
32+
void Do2();
33+
}
34+
}

src/Ninject.MockingKernel.Test/IntegrationTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ public void ToMockCanBeUsedToConfigureMocks()
6262
}
6363
}
6464

65+
/// <summary>
66+
/// The ToMock extension can be used to add additional interfaces.
67+
/// </summary>
68+
[Fact]
69+
public void ToMockCanBeUsedToAddAdditionalInterfaces()
70+
{
71+
using (var kernel = this.CreateKernel())
72+
{
73+
kernel.Bind<IDummyService>().ToMock(typeof(IDummyService2));
74+
75+
var mock = kernel.Get<IDummyService>();
76+
77+
mock.Should().BeAssignableTo<IDummyService2>();
78+
}
79+
}
80+
6581
/// <summary>
6682
/// Reals the objects are created for auto bindable types.
6783
/// </summary>

src/Ninject.MockingKernel.Test/Ninject.MockingKernel.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</ItemGroup>
7272
<ItemGroup>
7373
<Compile Include="IDummyService.cs" />
74+
<Compile Include="IDummyService2.cs" />
7475
<Compile Include="IntegrationTest.cs" />
7576
<Compile Include="Properties\AssemblyInfo.cs" />
7677
</ItemGroup>

0 commit comments

Comments
 (0)