diff --git a/src/NzbDrone.Common/Sonarr.Common.csproj b/src/NzbDrone.Common/Sonarr.Common.csproj
index ac5011cac..3d0cfb7b4 100644
--- a/src/NzbDrone.Common/Sonarr.Common.csproj
+++ b/src/NzbDrone.Common/Sonarr.Common.csproj
@@ -4,7 +4,7 @@
ISMUSL
-
+
diff --git a/src/NzbDrone.Host/Sonarr.Host.csproj b/src/NzbDrone.Host/Sonarr.Host.csproj
index fbb912170..9ffdd1129 100644
--- a/src/NzbDrone.Host/Sonarr.Host.csproj
+++ b/src/NzbDrone.Host/Sonarr.Host.csproj
@@ -9,8 +9,8 @@
-
-
+
+
diff --git a/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs b/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs
index 67a07a1dd..6e6d84666 100644
--- a/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs
+++ b/src/NzbDrone.Test.Common/AutoMoq/AutoMoqer.cs
@@ -3,18 +3,11 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
-using System.Linq.Expressions;
using System.Reflection;
-using System.Runtime.CompilerServices;
+using DryIoc;
using Moq;
-using Moq.Language.Flow;
using NzbDrone.Common.Composition;
using NzbDrone.Common.EnvironmentInfo;
-using NzbDrone.Test.Common.AutoMoq.Unity;
-using Unity;
-using Unity.Resolution;
-
-[assembly: InternalsVisibleTo("AutoMoq.Tests")]
namespace NzbDrone.Test.Common.AutoMoq
{
@@ -22,32 +15,18 @@ namespace NzbDrone.Test.Common.AutoMoq
public class AutoMoqer
{
public readonly MockBehavior DefaultBehavior = MockBehavior.Default;
- public Type ResolveType;
- private IUnityContainer _container;
+ private IContainer _container;
private IDictionary _registeredMocks;
public AutoMoqer()
{
- SetupAutoMoqer(new UnityContainer());
- }
-
- public AutoMoqer(MockBehavior defaultBehavior)
- {
- DefaultBehavior = defaultBehavior;
- SetupAutoMoqer(new UnityContainer());
- }
-
- public AutoMoqer(IUnityContainer container)
- {
- SetupAutoMoqer(container);
+ SetupAutoMoqer(CreateTestContainer(new Container()));
}
public virtual T Resolve()
{
- ResolveType = typeof(T);
var result = _container.Resolve();
SetConstant(result);
- ResolveType = null;
return result;
}
@@ -60,7 +39,6 @@ namespace NzbDrone.Test.Common.AutoMoq
public virtual Mock GetMock(MockBehavior behavior)
where T : class
{
- ResolveType = null;
var type = GetTheMockType();
if (GetMockHasNotBeenCalledForThisType(type))
{
@@ -79,89 +57,88 @@ namespace NzbDrone.Test.Common.AutoMoq
public virtual void SetMock(Type type, Mock mock)
{
- if (_registeredMocks.ContainsKey(type) == false)
+ if (GetMockHasNotBeenCalledForThisType(type))
{
_registeredMocks.Add(type, mock);
}
if (mock != null)
{
- _container.RegisterInstance(type, mock.Object);
+ _container.RegisterInstance(type, mock.Object, ifAlreadyRegistered: IfAlreadyRegistered.Replace);
}
}
public virtual void SetConstant(T instance)
{
- _container.RegisterInstance(instance);
+ _container.RegisterInstance(instance, ifAlreadyRegistered: IfAlreadyRegistered.Replace);
SetMock(instance.GetType(), null);
}
- public ISetup Setup(Expression> expression)
- where T : class
+ private IContainer CreateTestContainer(IContainer container)
{
- return GetMock().Setup(expression);
+ var c = container.CreateChild(IfAlreadyRegistered.Replace,
+ container.Rules
+ .WithDynamicRegistration((serviceType, serviceKey) =>
+ {
+ // ignore services with non-default key
+ if (serviceKey != null)
+ {
+ return null;
+ }
+
+ if (serviceType == typeof(object))
+ {
+ return null;
+ }
+
+ if (serviceType.IsGenericType && serviceType.IsOpenGeneric())
+ {
+ return null;
+ }
+
+ if (serviceType == typeof(System.Text.Json.Serialization.JsonConverter))
+ {
+ return null;
+ }
+
+ // get the Mock object for the abstract class or interface
+ if (serviceType.IsInterface || serviceType.IsAbstract)
+ {
+ var mockType = typeof(Mock<>).MakeGenericType(serviceType);
+ var mockFactory = DelegateFactory.Of(r =>
+ {
+ var mock = (Mock)r.Resolve(mockType);
+ SetMock(serviceType, mock);
+ return mock.Object;
+ }, Reuse.Singleton);
+
+ return new[] { new DynamicRegistration(mockFactory, IfAlreadyRegistered.Keep) };
+ }
+
+ // concrete types
+ var concreteTypeFactory = serviceType.ToFactory(Reuse.Singleton, FactoryMethod.ConstructorWithResolvableArgumentsIncludingNonPublic);
+
+ return new[] { new DynamicRegistration(concreteTypeFactory) };
+ },
+ DynamicRegistrationFlags.Service | DynamicRegistrationFlags.AsFallback));
+
+ c.Register(typeof(Mock<>), Reuse.Singleton, FactoryMethod.DefaultConstructor());
+
+ return c;
}
- public ISetup Setup(Expression> expression)
- where T : class
- {
- return GetMock().Setup(expression);
- }
-
- public void Verify(Expression> expression)
- where T : class
- {
- GetMock().Verify(expression);
- }
-
- public void Verify(Expression> expression, string failMessage)
- where T : class
- {
- GetMock().Verify(expression, failMessage);
- }
-
- public void Verify(Expression> expression, Times times)
- where T : class
- {
- GetMock().Verify(expression, times);
- }
-
- public void Verify(Expression> expression, Times times, string failMessage)
- where T : class
- {
- GetMock().Verify(expression, times, failMessage);
- }
-
- public void VerifyAllMocks()
- {
- foreach (var registeredMock in _registeredMocks)
- {
- if (registeredMock.Value is Mock mock)
- {
- mock.VerifyAll();
- }
- }
- }
-
- private void SetupAutoMoqer(IUnityContainer container)
+ private void SetupAutoMoqer(IContainer container)
{
_container = container;
container.RegisterInstance(this);
_registeredMocks = new Dictionary();
- RegisterPlatformLibrary(container);
- AddTheAutoMockingContainerExtensionToTheContainer(container);
+ LoadPlatformLibrary();
AssemblyLoader.RegisterNativeResolver(new[] { "System.Data.SQLite", "Sonarr.Core" });
}
- private static void AddTheAutoMockingContainerExtensionToTheContainer(IUnityContainer container)
- {
- container.AddNewExtension();
- return;
- }
-
private Mock TheRegisteredMockForThisType(Type type)
where T : class
{
@@ -178,7 +155,7 @@ namespace NzbDrone.Test.Common.AutoMoq
private bool GetMockHasNotBeenCalledForThisType(Type type)
{
- return _registeredMocks.ContainsKey(type) == false;
+ return !_registeredMocks.ContainsKey(type);
}
private static Type GetTheMockType()
@@ -187,7 +164,7 @@ namespace NzbDrone.Test.Common.AutoMoq
return typeof(T);
}
- private void RegisterPlatformLibrary(IUnityContainer container)
+ private void LoadPlatformLibrary()
{
var assemblyName = "Sonarr.Windows";
diff --git a/src/NzbDrone.Test.Common/AutoMoq/Unity/AutoMockingBuilderStrategy.cs b/src/NzbDrone.Test.Common/AutoMoq/Unity/AutoMockingBuilderStrategy.cs
deleted file mode 100644
index 6190ae70d..000000000
--- a/src/NzbDrone.Test.Common/AutoMoq/Unity/AutoMockingBuilderStrategy.cs
+++ /dev/null
@@ -1,79 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Reflection;
-using Moq;
-using Unity;
-using Unity.Builder;
-using Unity.Strategies;
-
-namespace NzbDrone.Test.Common.AutoMoq.Unity
-{
- public class AutoMockingBuilderStrategy : BuilderStrategy
- {
- private readonly IUnityContainer _container;
- private readonly MockRepository _mockFactory;
- private readonly IEnumerable _registeredTypes;
-
- public AutoMockingBuilderStrategy(IEnumerable registeredTypes, IUnityContainer container)
- {
- var autoMoqer = container.Resolve();
- _mockFactory = new MockRepository(autoMoqer.DefaultBehavior);
- _registeredTypes = registeredTypes;
- _container = container;
- }
-
- public override void PreBuildUp(ref BuilderContext context)
- {
- var autoMoqer = _container.Resolve();
-
- var type = GetTheTypeFromTheBuilderContext(context);
- if (AMockObjectShouldBeCreatedForThisType(type))
- {
- var mock = CreateAMockObject(type);
- context.Existing = mock.Object;
- autoMoqer.SetMock(type, mock);
- }
- }
-
- private bool AMockObjectShouldBeCreatedForThisType(Type type)
- {
- var mocker = _container.Resolve();
- return TypeIsNotRegistered(type) && (mocker.ResolveType == null || mocker.ResolveType != type);
- }
-
- private static Type GetTheTypeFromTheBuilderContext(BuilderContext context)
- {
- return context.Type;
- }
-
- private bool TypeIsNotRegistered(Type type)
- {
- return _registeredTypes.Any(x => x.Equals(type)) == false;
- }
-
- private Mock CreateAMockObject(Type type)
- {
- var createMethod = GenerateAnInterfaceMockCreationMethod(type);
-
- return InvokeTheMockCreationMethod(createMethod);
- }
-
- private Mock InvokeTheMockCreationMethod(MethodInfo createMethod)
- {
- return (Mock)createMethod.Invoke(_mockFactory, new object[] { new List
diff --git a/src/NzbDrone.Update/Sonarr.Update.csproj b/src/NzbDrone.Update/Sonarr.Update.csproj
index dd7fc25f9..8e86cce02 100644
--- a/src/NzbDrone.Update/Sonarr.Update.csproj
+++ b/src/NzbDrone.Update/Sonarr.Update.csproj
@@ -4,8 +4,8 @@
net6.0
-
-
+
+