Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对于不同类型的对象,应该是等效的_C#_Fluent Assertions - Fatal编程技术网

C# 对于不同类型的对象,应该是等效的

C# 对于不同类型的对象,应该是等效的,c#,fluent-assertions,C#,Fluent Assertions,在我的测试中,我得到了类型HttpRequestMessage的结果,我需要断言它的属性Content设置为correct object 问题在于HttpRequestMessage.Content与我要比较的对象具有不同的(基本)类型,我不能使用shouldbeeequivalentto,包括以下内容: HttpRequestMessage result = ... result.Content.ShouldBeEquivalentTo (new ObjectContent (obj.Get

在我的测试中,我得到了类型
HttpRequestMessage
的结果,我需要断言它的属性
Content
设置为correct object

问题在于
HttpRequestMessage.Content
与我要比较的对象具有不同的(基本)类型,我不能使用shouldbeeequivalentto,包括以下内容:

HttpRequestMessage result = ...

result.Content.ShouldBeEquivalentTo (new ObjectContent (obj.GetType (), obj, new JsonMediaTypeFormatter ()),
                                     options => options.Including (x => x.Value));
result.Should ().BeOfType<ObjectContent> ();

((ObjectContent) result.Content).ShouldBeEquivalentTo (new ObjectContent (obj.GetType (), obj, new JsonMediaTypeFormatter ()),
                                                        options => options.Including (x => x.Value));
这不会编译,因为选项使用的是内容属性类型(即
HttpContent
),而不是
ObjectContent

我发现的唯一方法是有两个这样的断言:

HttpRequestMessage result = ...

result.Content.ShouldBeEquivalentTo (new ObjectContent (obj.GetType (), obj, new JsonMediaTypeFormatter ()),
                                     options => options.Including (x => x.Value));
result.Should ().BeOfType<ObjectContent> ();

((ObjectContent) result.Content).ShouldBeEquivalentTo (new ObjectContent (obj.GetType (), obj, new JsonMediaTypeFormatter ()),
                                                        options => options.Including (x => x.Value));
result.Should().BeOfType();
((ObjectContent)result.Content)。应与(新的ObjectContent(obj.GetType(),obj,新的JsonMediaTypeFormatter())等效,
选项=>options.包括(x=>x.Value));

有更好的方法吗?可能是某种类型的
BeOfType
,它返回的是casted object fluent断言,而不是基本断言?

我不确定是否有更简单的方法,但如果您试图在多个位置避免难看的代码,扩展方法可能会很好:

类似于(我不确定这是否会按原样编译):

公共静态类应提供帮助
{
public static void ShouldBeSameContent(此HttpRequestMessage结果,应为对象)
{
result.Should().BeOfType();
((ObjectContent)result.Content).应与(新的ObjectContent(应为.GetType(),应为,新的JsonMediaTypeFormatter(),相同),
选项=>options.包括(x=>x.Value));
}
}

不能使用为您提供
ISubjectInfo
对象的重载?然后您可以使用基于文本的属性路径匹配。丹尼斯,如果您是指反射,那么我认为它会更难看。不,不,有一个重载为您提供ISubjectInfo的Func。该对象提供对完整属性的访问我认为你可能会得到同样的效果。看看第533行的例子。啊,我不知道。但这是神奇的字符串,在重构时会被破坏:(我同意。但这就是为什么首先要进行单元测试;-)嗯,是的,我们可以使用扩展,但我认为应该可以使用库本身,而不是一直编写扩展。