You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
1.2 KiB
37 lines
1.2 KiB
3 years ago
|
using System.Diagnostics.CodeAnalysis;
|
||
|
using System.Reflection;
|
||
|
using Autofac;
|
||
|
using AutoFixture;
|
||
3 years ago
|
|
||
2 years ago
|
namespace Recyclarr.TestLibrary.AutoFixture;
|
||
3 years ago
|
|
||
3 years ago
|
[SuppressMessage("Design", "CA1019", MessageId = "Define accessors for attribute arguments")]
|
||
|
public sealed class AutoMockDataAttribute : AutoDataAttribute
|
||
3 years ago
|
{
|
||
|
public AutoMockDataAttribute()
|
||
|
: base(NSubstituteFixture.Create)
|
||
|
{
|
||
|
}
|
||
3 years ago
|
|
||
|
public AutoMockDataAttribute(Type testFixtureClass, string methodName)
|
||
|
: base(() => CreateWithAutofac(testFixtureClass, methodName))
|
||
|
{
|
||
|
}
|
||
|
|
||
|
private static Fixture CreateWithAutofac(Type testFixtureClass, string methodName)
|
||
|
{
|
||
|
var method = testFixtureClass.GetMethod(methodName,
|
||
|
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
|
||
2 years ago
|
var getContainer = method?.CreateDelegate<Func<ILifetimeScope>>();
|
||
3 years ago
|
if (getContainer is null)
|
||
|
{
|
||
|
throw new ArgumentException("Unable to find method on test fixture. Method must be non-public to be found.",
|
||
|
nameof(methodName));
|
||
|
}
|
||
|
|
||
|
var fixture = NSubstituteFixture.Create();
|
||
|
fixture.Customizations.Add(new AutofacSpecimenBuilder(getContainer()));
|
||
|
return fixture;
|
||
|
}
|
||
3 years ago
|
}
|