如何断言转换器是否应用于使用 Fluent Assertions 的类型
How to assert if converter is applied on a type using Fluent Assertions
我需要应用一些复杂的逻辑来创建地图,所以我使用了如下自定义类型转换器:
cfg.CreateMap<ConsumerRequest,IEnumerable<ProviderRequest>>().ConvertUsing<RequestConverter>();
我在单元测试中使用 fluentassertions 并想断言 ConsumerRequest 已应用 RequestConverter 但不确定如何去做,我想我应该能够正确的扩展方法,该方法将对某些消息断言相同但不是确定如何实现这一目标,我们将不胜感激。
在 IMapper 上创建了如下扩展方法:
public static AutoMapperTypeConverterAssertions<TDestinationModel>ForType<TDestinationModel>(this IMapper subject)
{
return new AutoMapperTypeConverterAssertions<TDestinationModel>(subject);
}
public class AutoMapperTypeConverterAssertions<TDestinationModel> : ReferenceTypeAssertions<object, AutoMapperTypeConverterAssertions<TDestinationModel>>
{
public AutoMapperTypeConverterAssertions(object subject)
{
Subject = subject;
}
protected override string Context => "auto mapper type converter";
public AndConstraint<AutoMapperTypeConverterAssertions<TDestinationModel>> ShouldHaveTypeConverterApplied<TSourceModel, TConverterType>(
string because = "", params object[] becauseArgs)
{
var mapper = Subject as IMapper;
var foundTypeInConfig = mapper
.ConfigurationProvider
.FindTypeMapFor<TSourceModel, TDestinationModel>()
.TypeConverterType;
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(foundTypeInConfig == typeof(TConverterType))
.FailWith("Expected the mapping for {0} to contain {1} AutoMapper Type Converter, but was not found.", typeof(TDestinationModel).Name, typeof(TConverterType).Name);
}
return new AndConstraint<AutoMapperTypeConverterAssertions<TDestinationModel>>(this);
}
我需要应用一些复杂的逻辑来创建地图,所以我使用了如下自定义类型转换器:
cfg.CreateMap<ConsumerRequest,IEnumerable<ProviderRequest>>().ConvertUsing<RequestConverter>();
我在单元测试中使用 fluentassertions 并想断言 ConsumerRequest 已应用 RequestConverter 但不确定如何去做,我想我应该能够正确的扩展方法,该方法将对某些消息断言相同但不是确定如何实现这一目标,我们将不胜感激。
在 IMapper 上创建了如下扩展方法:
public static AutoMapperTypeConverterAssertions<TDestinationModel>ForType<TDestinationModel>(this IMapper subject)
{
return new AutoMapperTypeConverterAssertions<TDestinationModel>(subject);
}
public class AutoMapperTypeConverterAssertions<TDestinationModel> : ReferenceTypeAssertions<object, AutoMapperTypeConverterAssertions<TDestinationModel>>
{
public AutoMapperTypeConverterAssertions(object subject)
{
Subject = subject;
}
protected override string Context => "auto mapper type converter";
public AndConstraint<AutoMapperTypeConverterAssertions<TDestinationModel>> ShouldHaveTypeConverterApplied<TSourceModel, TConverterType>(
string because = "", params object[] becauseArgs)
{
var mapper = Subject as IMapper;
var foundTypeInConfig = mapper
.ConfigurationProvider
.FindTypeMapFor<TSourceModel, TDestinationModel>()
.TypeConverterType;
Execute.Assertion
.BecauseOf(because, becauseArgs)
.ForCondition(foundTypeInConfig == typeof(TConverterType))
.FailWith("Expected the mapping for {0} to contain {1} AutoMapper Type Converter, but was not found.", typeof(TDestinationModel).Name, typeof(TConverterType).Name);
}
return new AndConstraint<AutoMapperTypeConverterAssertions<TDestinationModel>>(this);
}