I'm writing in javascript with 'should' library, but this doesn't matter.

I should write test for method, which returns a quite big object with a lot of fields. How I should write my expected result?

I just wrote the same big object in expected, but it about 100 lines of code and looks not good.

Any suggestion? Or should I refactor my method in some way? At this moment method calls to 4 another methods, and collect results in some way to one object.

2 Answers 2


Refactor the 4 other methods into a separate class (could be one class per method, or one class with 4 methods). Chances are the class that is creating the one big object has too many responsibilities and you should split those up.

Test the individual methods to confirm that they work. The result of those calls will be a lot smaller than the big object.

Test the big method by asserting that the big method calls the 4 extracted methods and correctly assigns the values.

  • >correctly assigns the values. What do you mean?
    – podliy16
    Sep 4, 2015 at 12:58
  • @podliy16 divide and conquer: cover the pieces with individual tests, then assume valid input for the big test, which then only covers the interaction of 4 valid smaller pieces, not 4 potentially problematic pieces.
    – null
    Sep 4, 2015 at 13:22
  • I think I understood, yeah, quite good solution.
    – podliy16
    Sep 4, 2015 at 14:02

Are the four methods - and the data returned - dependent upon, and related to, each other?

If there is no real dependency, then expose those methods as public ones and allow each to return a smaller set of data, which can be independently tested.

If there is a strong dependency, consider making your large object hierarchical. So it may have four fields, each of which is an object with its own collection of fields. Again you can then either split the testing out into four tests and test each sub-object in turn, or you could have four separate should invocations (assuming should.js allows multiple asserts in one test; it's not a testing framework I'm familiar with).

  • Yes, they're strong related. I already have tests for these 'small' methods, But I don't want to change format which returns this big method, because this class already in use. And in current format there's mess with data so I can't clearly separate 4 parts/fields.
    – podliy16
    Sep 4, 2015 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.