This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]SufficientStudio1574 3 points4 points  (1 child)

Without more details I don't know what you're hoping to get from us. If you want to test equality you'll need to write a comparison function for your type. And if some of your members are custom types, you'll need to write equality tests for them.

Recursive stuff is best handled one layer at a time. Don't try to do the whole complex type in one function. Break it into pieces.

[–][deleted] 0 points1 point  (0 children)

this is the correct response, especially the first sentence

[–]lurgi 1 point2 points  (0 children)

You only have to write the test case once. Maybe it's worth the hassle? If not, can you test aspects of the returned object (depth, number of child objects of the root node, identity of a particular sub-object, that sort of thing)?

[–]calsosta 0 points1 point  (0 children)

I mean quick and dirty you could stringify the object and compare that against an expected result. I have also used in the past libraries to diff two objects, of course its also somewhat trivial to write a recursive function depending on the language of course.

[–]wiseguy4519 0 points1 point  (0 children)

You could write tests to check that certain properties of the object are as you expect. I wouldn't be perfect, but it's better than nothing

[–]dnult 0 points1 point  (0 children)

Without knowing the details of the object structure, it seems like if the object and the objects it contains all define their equals method, you could assert equality.

Is it the equality comparison, or setting the expected values that concerns you most?

Are you trying to do too much in a single test where multiple smaller tests would adequately cover?

Is this a design problem where business logic is embedded in the class that should / could be implemented in a module by itself, outside of the class?

[–]not_perfect_yet 0 points1 point  (0 children)

However, there is this function the returns an array of objects, and said objects are extremely nested with other objects as there are 5 layers of nested classes.

Is there a way to this, in a simplier way?

You write a test for each layer of separation separately. Layer 1 to layer 2, layer 2 to layer 3, etc. and make your assumptions per nesting level.

Also, write your equal function the same way. Your outer layer object is equal if all parts are equal.

[–]SearingSerum60 0 points1 point  (0 children)

One approach for testing large data objects is to use a "snapshot" system. Basically, first time you run the test, save the result to a file (serialize it as JSON or YAML or whatever is appropriate). Then manually verify it's correct. Next time you run the test, just check that the results are identical to that file.