I noticed with the latest nuget package (FluentAssertions -Version 5.6.0) I still can't see the ShouldBeEquivalentTo
method to do the object comparison without value (ONLY the structure). I can see Should().BeEquivalentTo()
but not sure how to do assert object structure except values.
My solution is on .Net Core 2.1
My code:
[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();
}
The last step of the above is called method below to assert the object model without data.
public async Task TheReturnedContentIs<T>(T obj)
{
var responseString = await ResponseMessage.Content.ReadAsStringAsync();
var deserializeObject = JsonConvert.DeserializeObject<T>(responseString);
obj.Should().BeEquivalentTo(deserializeObject);
}
Expected response:
{
"attributeA": {
"attribute2": "CITIPP",
"attribute3": [
{
"attribute4": "Variable",
"attribute5": {
"attribute6": 65.5022916666667,
"attribute7": 45.407291666666673,
"attribute8": 33.454791666666679
}
},
{
"attribute4": "Fixed",
"attribute5": {
"attribute6": 21.8762916666667,
"attribute7": 89.432291666666673,
"attribute8": 90.236791666666679
}
}
]
},
"attributeB": {
"attribute2": "CITIPP",
"attribute3": [
{
"attribute4": "Variable",
"attribute5": {
"attribute6": 71.5022916666667,
"attribute7": 53.407291666666673,
"attribute8": 62.454791666666679
}
},
{
"attribute4": "Fixed",
"attribute5": {
"attribute6": 38.5022916666667,
"attribute7": 53.407291666666673,
"attribute8": 44.3684791666666679
}
}
]
}
}
With the assertion below actual response should pass. As you noticed structure is same not the values. But if any of the attribute
name is different assertion should fail. (e.g. if attributeA
changed to attributeX
in the response)
Actual Response
{
"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
}
}
]
}
}
I pass the object model to the above method. So I expect to compare the object structure without the values. The reason for above assertion failure due to value differences. The structure is matched perfectly.