【发布时间】:2019-12-14 19:31:28
【问题描述】:
我已经开始在我的 REST 端点集成测试中使用 FluentAssertions 库。问题是我必须比较两个实体,但不包括它们的 _id 属性。这个属性是从我的IEntity 接口继承而来的。
public interface IEntity
{
[BsonId]
ObjectId _id { get; set; }
}
所以例如Log 类看起来像这样
[DataContract]
public class Log : IEntity
{
[BsonId]
public ObjectId _id { get; set; }
public string Message { get; set; }
}
在测试中我像这样比较它们并且它有效
retrieved.Should()
.BeEquivalentTo(expected, options => options.Excluding(member => member._id));
但是当我将此功能提取到扩展方法中以供重用时,它不起作用。它不会忽略_id 成员。
public static class ObjectAssertionExtensions
{
public static void BeEquivalentToExcludingId<TExpectation>(this ObjectAssertions objectAssertion, TExpectation expectation) where TExpectation : IEntity
{
objectAssertion.BeEquivalentTo(expectation, options => options.Excluding(member => member._id));
}
}
当我将通用扩展方法更改为特定类型 Log 时,它会正常工作。
我已经准备了最小的项目,例如here。
请问有没有办法让它工作,为什么它不能正常工作?我将尝试在 github 存储库中检查 FluentAssertions 的代码。谢谢。
【问题讨论】:
-
在
BeEquivalentTo中使用之前将expected转换为IEntity会有什么不同吗? -
不,它给了我“消息:System.InvalidOperationException:没有找到要比较的成员。请指定一些要包含在比较中的成员或选择更有意义的断言。”
-
可能是github.com/fluentassertions/fluentassertions/issues/1077的情况。已在 master 中修复,但尚未发布。
标签: c# nunit integration-testing extension-methods fluent-assertions