1
0
Fork 0
mirror of https://github.com/Sonarr/Sonarr synced 2024-12-23 00:07:07 +00:00

Added Ninject.Moq

This commit is contained in:
kay.one 2010-09-26 17:22:44 -07:00
parent d870c82147
commit 7540890987
5 changed files with 181 additions and 0 deletions

View file

@ -0,0 +1,33 @@
using System;
using Ninject.Infrastructure;
using Ninject.Injection;
using Ninject.Planning.Bindings;
using Ninject.Syntax;
namespace Ninject.Moq
{
/// <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;
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()));
IBinding binding = haveBinding.Binding;
binding.ProviderCallback = ctx => new MockProvider(ctx.Kernel.Components.Get<IInjectorFactory>());
return builder as IBindingWhenInNamedWithOrOnSyntax<T>;
}
}
}

View file

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using Moq;
using Ninject.Activation;
using Ninject.Injection;
namespace Ninject.Moq
{
/// <summary>
/// Creates mocked instances via Moq.
/// </summary>
public class MockProvider : IProvider
{
private static readonly Dictionary<Type, ConstructorInjector> _injectors = new Dictionary<Type, ConstructorInjector>();
/// <summary>
/// Gets the type (or prototype) of instances the provider creates.
/// </summary>
public Type Type
{
get { return typeof(Mock<>); }
}
/// <summary>
/// Gets the injector factory component.
/// </summary>
public IInjectorFactory InjectorFactory { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="MockProvider"/> class.
/// </summary>
/// <param name="injectorFactory">The injector factory component.</param>
public MockProvider(IInjectorFactory injectorFactory)
{
InjectorFactory = injectorFactory;
}
/// <summary>
/// Creates an instance within the specified context.
/// </summary>
/// <param name="context">The context.</param>
/// <returns>The created instance.</returns>
public object Create(IContext context)
{
ConstructorInjector injector = GetInjector(context.Request.Service);
var mock = injector.Invoke() as Mock;
return mock.Object;
}
private ConstructorInjector GetInjector(Type service)
{
lock (_injectors)
{
Type mockType = typeof(Mock<>).MakeGenericType(service);
if (_injectors.ContainsKey(mockType))
return _injectors[mockType];
ConstructorInjector injector = InjectorFactory.Create(mockType.GetConstructor(Type.EmptyTypes));
_injectors[mockType] = injector;
return injector;
}
}
/// <summary>
/// Gets a callback that creates an instance of the <see cref="MockProvider"/>.
/// </summary>
/// <returns>The created callback.</returns>
public static Func<IContext, IProvider> GetCreationCallback()
{
return ctx => new MockProvider(ctx.Kernel.Components.Get<IInjectorFactory>());
}
}
}

View file

@ -0,0 +1,41 @@
using System;
using Ninject.Activation.Caching;
using Ninject.Planning.Bindings;
namespace Ninject.Moq
{
/// <summary>
/// A kernel that will create mocked instances (via Moq) for any service that is
/// requested for which no binding is registered.
/// </summary>
public class MockingKernel : StandardKernel
{
/// <summary>
/// Clears the kernel's cache, immediately deactivating all activated instances regardless of scope.
/// This does not remove any modules, extensions, or bindings.
/// </summary>
public void Reset()
{
Components.Get<ICache>().Clear();
}
/// <summary>
/// Attempts to handle a missing binding for a service.
/// </summary>
/// <param name="service">The service.</param>
/// <returns><c>True</c> if the missing binding can be handled; otherwise <c>false</c>.</returns>
protected override bool HandleMissingBinding(Type service)
{
var binding = new Binding(service)
{
ProviderCallback = MockProvider.GetCreationCallback(),
ScopeCallback = ctx => null,
IsImplicit = true
};
AddBinding(binding);
return true;
}
}
}

View file

@ -42,6 +42,10 @@
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>Moq\Moq.dll</HintPath> <HintPath>Moq\Moq.dll</HintPath>
</Reference> </Reference>
<Reference Include="Ninject, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\NzbDrone.Core\Libraries\Ninject.dll</HintPath>
</Reference>
<Reference Include="SubSonic.Core, Version=3.0.0.3, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="SubSonic.Core, Version=3.0.0.3, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\NzbDrone.Core\Libraries\SubSonic.Core.dll</HintPath> <HintPath>..\NzbDrone.Core\Libraries\SubSonic.Core.dll</HintPath>
@ -59,7 +63,11 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="DbConfigControllerTest.cs" /> <Compile Include="DbConfigControllerTest.cs" />
<Compile Include="Ninject.Moq\ExtensionsForBindingSyntax.cs" />
<Compile Include="Ninject.Moq\MockingKernel.cs" />
<Compile Include="Ninject.Moq\MockProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SeriesTest.cs" />
<Compile Include="TvDbControllerTest.cs" /> <Compile Include="TvDbControllerTest.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Text;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using Moq;
using NzbDrone.Core.Controllers;
// ReSharper disable InconsistentNaming
namespace NzbDrone.Core.Test
{
[TestFixture]
public class SeriesTest
{
[Test]
[Description("This test will confirm that a folder will be skipped if it has been resolved to a series already assigned to another folder")]
public void skip_same_series_diffrent_folder()
{
//Arrange
var seriesProvider = new SeriesController(new Mock<Ilog>(), new Mock<IDiskController>(), new Mock<IConfigController>(), new )
}
}
}