c# – 将枚举与自定义Fluent断言等效步骤进行比较
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 将枚举与自定义Fluent断言等效步骤进行比较,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3736字,纯文字阅读大概需要6分钟。
内容图文
![c# – 将枚举与自定义Fluent断言等效步骤进行比较](/upload/InfoBanner/zyjiaocheng/780/4cc64deaa95a404db1d37c0dadbff830.jpg)
我正在尝试为Fluent Assertions编写自定义Equivalency Step,以将主题侧的Enum值与异常侧的字符串进行比较.
我似乎面临的问题是,在调用EquivalencyStep之前,传入IEquivalencyStep的主题类型已转换为字符串.
在流畅的断言中是否有一些魔法正在尝试将枚举直接转换为字符串?
代码示例如下:
public class SubjectClass
{
public EnumType Prop1 { get; set; }
}
public enum EnumType
{
A,
B,
C
}
public class ExpectionClass
{
public string Prop1 { get; set; }
}
[TestFixture]
public class ComparingEnumWithTests
{
[Test]
public void Test()
{
var expection = new ExpectionClass() {Prop1 = "bbb"};
var subject = new SubjectClass() {Prop1 = EnumType.B};
subject.ShouldBeEquivalentTo(expection, opt => opt.Using(new ComparingEnumWith<EnumType>(new Dictionary<string, EnumType>()
{
{"aaa", EnumType.A },
{"bbb", EnumType.B },
{"ccc", EnumType.C }
})));
}
}
public class ComparingEnumWith<TEnum> : IEquivalencyStep
where TEnum : struct
{
private readonly IReadOnlyDictionary<string, TEnum> _dictionary;
private readonly Type _enumType;
public ComparingEnumWith(IReadOnlyDictionary<string, TEnum> dictionary)
{
_enumType = typeof(TEnum);
if (!_enumType.IsEnum)
{
throw new ArgumentException("TEnum must be an enum");
}
_dictionary = dictionary;
}
public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config)
{
var subjectType = config.GetSubjectType(context);
return subjectType != null && subjectType == _enumType && context.Expectation is string;
}
public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
{
var expected = _dictionary[(string)context.Expectation];
return ((TEnum)context.Subject).Equals(expected);
}
}
更新:
从上面的问题我已经将代码调整到下面,其他任何人都遇到了同样的问题.
public class SubjectClass
{
public EnumType Prop1 { get; set; }
}
public enum EnumType
{
A,
B,
C
}
public class ExpectionClass
{
public string Prop1 { get; set; }
}
[TestFixture]
public class ComparingEnumWithTests
{
[Test]
public void Test()
{
var expection = new ExpectionClass() {Prop1 = "bbb"};
var subject = new SubjectClass() {Prop1 = EnumType.B};
AssertionOptions.EquivalencySteps.Insert<ComparingEnumWith<EnumTypeDataMapProvider, EnumType>>();
subject.ShouldBeEquivalentTo(expection);
}
}
public interface IEnumDataMapProvider<TEnum>
{
IReadOnlyDictionary<string, TEnum> Map { get; }
}
public class EnumTypeDataMapProvider : IEnumDataMapProvider<EnumType>
{
public IReadOnlyDictionary<string, EnumType> Map => new Dictionary<string, EnumType>()
{
{"aaa", EnumType.A},
{"bbb", EnumType.B},
{"ccc", EnumType.C}
};
}
public class ComparingEnumWith<TMapProvider, TEnum> : IEquivalencyStep
where TMapProvider : IEnumDataMapProvider<TEnum>
where TEnum : struct
{
private readonly IReadOnlyDictionary<string, TEnum> _dictionary;
private readonly Type _enumType;
public ComparingEnumWith()
{
_enumType = typeof(TEnum);
if (!_enumType.IsEnum)
{
throw new ArgumentException("TEnum must be an enum");
}
var provider = Activator.CreateInstance<TMapProvider>();
_dictionary = provider.Map;
}
public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config)
{
var subjectType = config.GetSubjectType(context);
return subjectType != null && subjectType == _enumType && context.Expectation is string;
}
public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
{
var expected = _dictionary[(string)context.Expectation];
return ((TEnum)context.Subject).Equals(expected);
}
}
解决方法:
那是一个错误.您正在注册我们称之为用户等效步骤的内容.但这些运行在内置的TryConversionEquivalencyStep和ReferenceEqualityEquivalencyStep之后.如果你能把它归档为bug,我们可以看一下.要解决此问题,请考虑使用AssertionOptions.EquivalencySteps.Insert< ComparingEnumWith< T>>();在所有内置步骤之前插入自定义步骤.
内容总结
以上是互联网集市为您收集整理的c# – 将枚举与自定义Fluent断言等效步骤进行比较全部内容,希望文章能够帮你解决c# – 将枚举与自定义Fluent断言等效步骤进行比较所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。