Is application-specific data required for good unit testing?

Posted by stinkycheeseman on Programmers See other posts from Programmers or by stinkycheeseman
Published on 2012-03-27T16:02:51Z Indexed on 2012/03/27 17:42 UTC
Read the original article Hit count: 199

Filed under:
|

I am writing unit tests for a fairly simple function that depends on a fairly complicated set of data. Essentially, the object I am manipulating represents a graph and this function determines whether to chart a line, bar, or pie chart based on the data that came back from the server.

This is a simplified version, using jQuery:

setDefaultChartType: function (graphObject) {
  var prop1 = graphObject.properties.key;
  var numCols = 0;
  $.each(graphObject.columns, function (colIndex, column) {
    numCols++;
  });

  if ( numCols > 6 || ( prop1 > 1 && graphObject.data.length == 1) ) {
    graphObject.setChartType("line");
  } else if ( numCols <=6 && prop1 == 1 ) {
    graphObject.setChartType("bar");
  } else if ( numCols <=6 && prop1 > 1 ) {
    graphObject.setChartType("pie");
  }
}

My question is, should I use mock data that is procured from the actual database? Or can I just fabricate data that fits the different cases?

I'm afraid that fabricating data will not expose bugs arising from changes in the database, but on the other hand, it would require a lot more effort to keep the test data up-to-date that I'm not sure is necessary.

© Programmers or respective owner

Related posts about unit-testing

Related posts about mocking