Rush Frisby
Rush Frisby

Reputation: 11454

Assert.AreEqual on two objects but ignore DateTime properties

I have a unit test where I mock the result of something which has a DateTime property which is set when I perform the action - a "CreatedOn" property. Since I can't create my result and perform the action at the same time my Assert.AreEqual always fails. How can I test that my two objects are the same Except for certain DateTime fields?

Upvotes: 2

Views: 1609

Answers (2)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

I usually do one of following things:

  • You can create TimeProvider dependency for your SUT and use that provider to get current date. That will allow you to setup any current date.
  • If object which you are comparing is returned by one of SUT dependencies, then you can setup mocked dependency to return exactly same object instance which you are checking in your test.

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564403

There are really two options here:

  1. Change CreatedOn after the creation time to make the properties match. This may or may not be possible given your API.
  2. Make a method that checks all of the other properties, but ignores the CreatedOn property. Just use that. This is effectively just multiple Assert.AreEqual calls, but on individual properties, not just the object as a whole.

Upvotes: 1

Related Questions