Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6335237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:49:00+00:00 2026-05-24T18:49:00+00:00

I have read in several book and articles about TDD and BDD that one

  • 0

I have read in several book and articles about TDD and BDD that one should avoid multiple assertions or expectations in a single unit test or specification. And I can understand the reasons for doing so. Still I am not sure what would be a good way to verify a complex result.

Assuming a method under test returns a complex object as a result (e.g. deserialization or database read) how do I verify the result correctly?

1.Asserting on each property:

  Assert.AreEqual(result.Property1, 1);
  Assert.AreEqual(result.Property2, "2");
  Assert.AreEqual(result.Property3, null);
  Assert.AreEqual(result.Property4, 4.0);

2.Relying on a correctly implemented .Equals():

Assert.AreEqual(result, expectedResult);

The disadvantage of 1. is that if the first assert fails all the following asserts are not run, which might have contained valuable information to find the problem. Maintainability might also be a problem as Properties come and go.

The disatvantage of 2. is that I seem to be testing more than one thing with this test. I might get false positives or negatives if .Equals() is not implemented correctly. Also with 2. I do not see, what properties are actually different if the test fails but I assume that can often be addressed with a decent .ToString() override. In any case I think I should avoid to be forced to throw the debugger at the failing tests to see the difference. I should see it right away.

The next problem with 2. is that it compares the whole object even though for some tests only some properties might be significant.

What would be a decent way or best practise for this in TDD and BDD.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:49:01+00:00Added an answer on May 24, 2026 at 6:49 pm

    With the context present in the question I’d go for option 1.

    It likely depends on context. If I’m using some sort of built in object serialization within the .NET framework, I can be reasonably assured that if no errors were encountered then the entire object was appropriately marshaled. In that case, asserting a single field in the object is probably fine. I trust MS libraries to do the right thing.

    If you are using SQL and manually mapping results to domain objects I feel that option 1 makes it quicker to diagnose when something breaks than option 2. Option 2 likely relies on toString methods in order to render the assertion failure:

    Expected <1 2 null 4.0> but was <1 2 null null>
    

    Now I am stuck trying to figure out what field 4.0/null was. Of course I could put the field name into the method:

    Expected <Property1: 1, Property2: 2, Property3: null, Property4: 4.0>
     but was <Property1: 1, Property2: 2, Property3: null, Property4: null>
    

    This is fine for small numbers of properties, but begins to break down larger numbers of properties due to wrapping, etc. Also, the toString maintenance could become an issue as it needs to change at the same rate as the equals method.

    Of course there is no correct answer, at the end of the day, it really boils down to your team’s (or your own) personal preference.

    Hope that helps!

    Brandon

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read below line in one book about buffered input Why have buffers? First,
First, I must say that I have read several post about this at StackOverflow
I have read several posts here about detach and attach. But it should be
I have read several articles on when to use nested classes, but none that
I have read several articles on the topic as this one and implemented most
I have read several articles about UITableView, including the official doc and some on
I have read several articles and questions on concept of foreign key vs independent
I have read several posts about the configuration manager in VS2010 (or before) but
I have read through several articles which are alternatives to using setpixel/getpixel but I
I have read in several places that it's possible to share the objects directory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.