Skip to content

Commit 8526d89

Browse files
committed
Fixed #17
support auto mocking named interfaces
1 parent f555674 commit 8526d89

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
9+
### Added
10+
- Support auto mocking named interfaces [#17](https://github.com/ninject/Ninject.MockingKernel/issues/17)
11+
712
## [3.3.0-beta1]
813

914
### Added

src/Ninject.MockingKernel.Test/IntegrationTest.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace Ninject.MockingKernel
2424
using FluentAssertions;
2525

2626
using Xunit;
27-
27+
2828
/// <summary>
2929
/// Abstract test base for testing mocking kernel implementations
3030
/// </summary>
@@ -126,6 +126,15 @@ public void TheInjectedDependenciesAreMocks()
126126
}
127127
}
128128

129+
[Fact]
130+
public void NamedDependencyCanBeAutoMocked()
131+
{
132+
using (var kernel = this.CreateKernel())
133+
{
134+
var instance = kernel.Get<DummyClassWithNamedDependency>();
135+
}
136+
}
137+
129138
/// <summary>
130139
/// Asserts that do was called.
131140
/// </summary>
@@ -168,5 +177,12 @@ public DummyClass(IDummyService dummyService, DummyDelegate dummyDelegate)
168177

169178
public DummyDelegate DummyDelegate { get; set; }
170179
}
180+
181+
public class DummyClassWithNamedDependency
182+
{
183+
public DummyClassWithNamedDependency([Named("Foo")] IDummyService dummyService)
184+
{
185+
}
186+
}
171187
}
172188
}

src/Ninject.MockingKernel/MockMissingBindingResolver.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Ninject.MockingKernel
1010
{
1111
using System;
1212
using System.Collections.Generic;
13+
using System.Linq;
1314
using Ninject.Activation;
1415
using Ninject.Components;
1516
using Ninject.Infrastructure;
@@ -47,13 +48,20 @@ public IEnumerable<IBinding> Resolve(Multimap<Type, IBinding> bindings, IRequest
4748
IList<IBinding> bindingList = new List<IBinding>();
4849
if (this.TypeIsInterfaceOrAbstract(service))
4950
{
50-
bindingList.Add(
51-
new Binding(service)
52-
{
53-
ProviderCallback = this.mockProviderCallbackProvider.GetCreationCallback(),
54-
ScopeCallback = ctx => StandardScopeCallbacks.Singleton,
55-
IsImplicit = true,
56-
});
51+
var binding = new Binding(service)
52+
{
53+
ProviderCallback = this.mockProviderCallbackProvider.GetCreationCallback(),
54+
ScopeCallback = ctx => StandardScopeCallbacks.Singleton,
55+
IsImplicit = true,
56+
};
57+
58+
if (request.Target != null &&
59+
request.Target.GetCustomAttributes(typeof(NamedAttribute), false).FirstOrDefault() is NamedAttribute namedAttribute)
60+
{
61+
binding.Metadata.Name = namedAttribute.Name;
62+
}
63+
64+
bindingList.Add(binding);
5765
}
5866

5967
return bindingList;

0 commit comments

Comments
 (0)