在这一点上,我的大脑被炸了,如果这很愚蠢,请您道歉:
我正在尝试在运行时使用反射收集实现接口类型的所有类型,并让AutoFixture创建它们的实例。其中一些没有无参数的ctor。
因此,我正在做一个
public static IEnumerable<T> GetTypesWithInterface<T>() where T : class
{
var iType = typeof(T);
return iType.Assembly.GetLoadableTypes() //another method which does as it's namesake implies
.Where(type => iType.IsAssignableFrom(type) && !type.IsInterface)
.Select(t => FormatterServices.GetUninitializedObject(t) as T)
.ToList();
}
以及其他地方使用a:]调用代码>
protected List<T> AutoCreateSpecimenWithFixture<T>() where T : class { return TypeLoaderExtensions.GetTypesWithInterface<T>() .Select(t => new SpecimenContext(Fixture).Resolve(t)) // .Select(t => Fixture.Create(t, specimen)) .Cast<T>() .ToList(); }
在此之前,我的灯具是:
protected UnitTest() { Fixture = new Fixture(); Fixture.Customize(new AutoMoqCustomization()); CustomRegistrations(); } private void CustomRegistrations() { _settingsServiceMock ??= Fixture.Freeze<Mock<ISettingsService>>(); Fixture.Register(() => new OverrideRule(_settingsServiceMock.Object)); }
我玩过上面的冻结/插入/注册,没有任何乐趣。在我的实际集成测试中,执行以下操作:
var kek = Fixture.Create<OverrideRule>(); //works _rules = AutoCreateSpecimenWithFixture<IRule>(); //throws no public ctor error :/
我也已经看过这些,但是还没有按照
AutoCreateSpecimenWithFixture
方法使它们工作:
AutoFixture: how to CreateAnonymous from a System.Type
AutoFixture: Unable to create an instance, probably no public constructor
请提供任何帮助,谢谢。
在这一点上,我的大脑被炸了,如果这很愚蠢,我深表歉意:我正在尝试收集所有类型,这些类型在运行时使用反射来实现接口类型,并让AutoFixture创建...