Asp.net core Fluent断言中的对象结构比较方法

Asp.net core Fluent断言中的对象结构比较方法,asp.net-core,fluent-assertions,Asp.net Core,Fluent Assertions,我注意到,使用最新的nuget软件包(FluentAssertions-5.6.0版),我仍然无法看到应该等效到方法来进行没有值的对象比较(只有结构)。我可以看到Should().beeEquivalento(),但不确定如何断言除值之外的对象结构 我的解决方案在上.Net Core 2.1 我的代码: [Fact] public void GetFlatTariffForOneProduct_ReturnSuccess() { this.

我注意到,使用最新的nuget软件包(FluentAssertions-5.6.0版),我仍然无法看到
应该等效到
方法来进行没有值的对象比较(只有结构)。我可以看到
Should().beeEquivalento()
,但不确定如何断言除值之外的对象结构

我的解决方案在
上.Net Core 2.1

我的代码:

[Fact]
        public void GetFlatTariffForOneProduct_ReturnSuccess()
        {
            this.Given(_ => _steps.GivenACorrelationIdHeaderIsProvided(true))
                .And(_ => _steps.TheFlatTariffRawDataInDb(1))
                .When(_ => _steps.WhenTheRequestExecutes(_endpoint, new EstimationRequestBuilder().FlatRateElecRequest().Build()))
                .Then(_ => _steps.TheResponseCodeIs(HttpStatusCode.OK))
                .And(_ => _steps.TheReturnedContentIs(new EstimationResponseBuilder().EstimateResponse(1).Build()))
                .BDDfy();
        }
上面的最后一步称为下面的方法来断言没有数据的对象模型

public async Task TheReturnedContentIs<T>(T obj)
        {
            var responseString = await ResponseMessage.Content.ReadAsStringAsync();
            var deserializeObject = JsonConvert.DeserializeObject<T>(responseString);
            obj.Should().BeEquivalentTo(deserializeObject);

        }
使用下面的断言,实际响应应该通过。正如您所注意到的,结构与值不同。但是如果
属性的任何一个
名称不同,则断言应该失败。(例如,如果响应中将
attributeA
更改为
attributeX

实际响应

{
    "attributeA": {
        "attribute2": "ABCD",
        "attribute3": [
            {
                "attribute4": "Variable",
                "attribute5": {
                    "attribute6": 71.5022916666667,
                    "attribute7": 53.407291666666673,
                    "attribute8": 62.454791666666679
                }
            },
            {
                "attribute4": "Fixed",
                "attribute5": {
                    "attribute6": 71.5022916666667,
                    "attribute7": 53.407291666666673,
                    "attribute8": 62.454791666666679
                }
            }
        ]
    },
    "attributeB": {
        "attribute2": "CITIPP",
        "attribute3": [
            {
                "attribute4": "Variable",
                "attribute5": {
                    "attribute6": 54.5022916666667,
                    "attribute7": 11.407291666666673,
                    "attribute8": 98.454791666666679
                }
            },
            {
                "attribute4": "Fixed",
                "attribute5": {
                    "attribute6": 71.222916666667,
                    "attribute7": 53.33291666666673,
                    "attribute8": 32.454791666666679
                }
            }
        ]
    }
}

我将对象模型传递给上述方法。因此,我希望比较没有值的对象结构。由于值差异导致上述断言失败的原因。结构完全匹配。

您可以像
BeEquivalentTo
通常那样遍历两个对象图,但通过将任意两个实例视为相等来忽略所有字符串和数字类型

这里有一个例子可以做到这一点

T expected=JsonConvert.DeserializeObject(expectedJSON);
T actual=JsonConvert.DeserializeObject(actualJSON);
actual.Should().beequivalento(预期值为opt=>opt
.使用(=>{})
.When(e=>e.RuntimeType.IsValueType)
.使用(=>{})
.WhenTypeIs())

阅读文档中的更多内容:

应与v5.0.0中删除的
等效。有关您的特定比较问题的帮助,请发布一个使用此
obj.Should().Equals(反序列化对象)的it。我不确定还有什么更好的。难道不是
obj.Should().Equals()
解析为
Object.Equals(Object)
?@JonasNyrup是的。那么你不再断言任何东西了吗?
{
    "attributeA": {
        "attribute2": "ABCD",
        "attribute3": [
            {
                "attribute4": "Variable",
                "attribute5": {
                    "attribute6": 71.5022916666667,
                    "attribute7": 53.407291666666673,
                    "attribute8": 62.454791666666679
                }
            },
            {
                "attribute4": "Fixed",
                "attribute5": {
                    "attribute6": 71.5022916666667,
                    "attribute7": 53.407291666666673,
                    "attribute8": 62.454791666666679
                }
            }
        ]
    },
    "attributeB": {
        "attribute2": "CITIPP",
        "attribute3": [
            {
                "attribute4": "Variable",
                "attribute5": {
                    "attribute6": 54.5022916666667,
                    "attribute7": 11.407291666666673,
                    "attribute8": 98.454791666666679
                }
            },
            {
                "attribute4": "Fixed",
                "attribute5": {
                    "attribute6": 71.222916666667,
                    "attribute7": 53.33291666666673,
                    "attribute8": 32.454791666666679
                }
            }
        ]
    }
}