Assertions comparing objects with difference in the datetime

DaveAngel

New member
Joined
Jan 7, 2021
Messages
2
Programming Experience
5-10
Hi,

Im currently comparing an object with below

Should().ContainEquivalentOf(myObj, options => options.Excluding(person => person.Id));

myObj has a property in it called Creation as DateTime and when comparing myObj with person there is a slight difference in the DateTime milliseconds. How is best to handle this without ignoring the Creation property in the object?

Thanks
 
@DaveAngel : When you initially posted your question, you should have mentioned that you are using Fluent Assertions. This will help save some confusion because when I first saw your question, I was thinking to myself: "I've never heard of Should() and ContainEquivalentOf() LINQ extension methods. Must be something the user wrote himself. He should post the code behind those."

Anyway, after a quick 3 minute scan through the Fluent Assertions docs, I found that the "Equivalency Comparison Behavior" and it looks like there are ways to compare times to be "close to" each other. From the docs:
C#:
orderDto.Should().BeEquivalentTo(order, options => options
    .Using<DateTime>(ctx => ctx.Subject.Should().BeCloseTo(ctx.Expectation, 1000))
    .When(info => info.SelectedMemberPath.EndsWith("Date")));


Perhaps you can use the same thing.
 
Hi - yes thanks for this. Just found it pretty much same time. In addition I found you can do this as a whole for all the datetime properties with

.WhenTypeIs<DateTime>());
 
Glad you found a solution. Moving this to testing frameworks sub-forum.
 
Back
Top Bottom