从理论上讲,应该可以填充自动模拟实例的属性。
假设类型的IPagination<T>
属性ViewModel
定义为:
public interface IPagination<T>
{
SearchFilter Property1 { get; set; }
string Property2 { get; set; }
}
我们可以创建一个临时的自动模拟自定义,例如MyCustomization
.
var fixture = new Fixture()
.Customize(new MyCustomization());
var context = new SpecimenContext(fixture.Compose());
然后,以下调用将创建一个实例ViewModel
(仅在运行时知道),提供一个自动模拟实例,IPagination<Data>
并为属性赋值。
var value = context.Resolve(typeof(ViewModel));
// List -> {IPagination`1Proxy593314cf4c134c5193c0019045c05a80}
// List.Property1.Name -> "Namef71b8571-a1a0-421d-9211-5048c96d891b"
// List.Property2 -> "f58cae65-b704-43ec-b2ce-582a5e6177e6"
我的定制
在应用此自定义之前,请记住这仅适用于此特定场景(因此描述中的临时)。我强烈建议在其他任何地方使用 Auto Mocking、AutoMoq、AutoRhinoMocks、AutoFakeItEasy或AutoNSubstitute的扩展之一。
internal class MyCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customizations.Add(new MySpecimenBuilder());
}
private class MySpecimenBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
var type = request as Type;
if (type == null || !type.IsInterface)
{
return new NoSpecimen(request);
}
object specimen = this
.GetType()
.GetMethod(
"Create",
BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(new[] { type })
.Invoke(this, new object[] { context });
return specimen;
}
private static object Create<TRequest>(ISpecimenContext context)
where TRequest : class
{
var mock = new Mock<TRequest>();
mock.SetupAllProperties();
foreach (PropertyInfo propInfo in typeof(TRequest).GetProperties())
{
object value = context.Resolve(propInfo.PropertyType);
propInfo.SetValue(mock.Object, value);
}
return mock.Object;
}
}
}