Sonarr/NzbDrone.Core.Test/Ninject.Moq/ExtensionsForBindingSyntax.cs

36 lines
1.3 KiB
C#
Raw Normal View History

2010-09-27 00:22:44 +00:00
using System;
using Ninject.Infrastructure;
using Ninject.Injection;
using Ninject.Planning.Bindings;
using Ninject.Syntax;
namespace Ninject.Moq
{
2011-04-10 02:44:01 +00:00
/// <summary>
/// Extensions for the fluent binding syntax API.
/// </summary>
public static class ExtensionsForBindingSyntax
{
/// <summary>
/// Indicates that the service should be bound to a mocked instance of the specified type.
/// </summary>
/// <typeparam name = "T">The service that is being mocked.</typeparam>
/// <param name = "builder">The builder that is building the binding.</param>
public static IBindingWhenInNamedWithOrOnSyntax<T> ToMock<T>(this IBindingToSyntax<T> builder)
{
var haveBinding = builder as IHaveBinding;
2010-09-27 00:22:44 +00:00
2011-04-10 02:44:01 +00:00
if (haveBinding == null)
throw new NotSupportedException(
String.Format(
"The binding builder for {0} is of type {1}, which does not implement IHaveBinding and is therefore not extensible.",
typeof (T), builder.GetType()));
2010-09-27 00:22:44 +00:00
2011-04-10 02:44:01 +00:00
IBinding binding = haveBinding.Binding;
2010-09-27 00:22:44 +00:00
2011-04-10 02:44:01 +00:00
binding.ProviderCallback = ctx => new MockProvider(ctx.Kernel.Components.Get<IInjectorFactory>());
return builder as IBindingWhenInNamedWithOrOnSyntax<T>;
}
}
}