Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using mocks #3

Open
MikeWarren2014 opened this issue Mar 7, 2018 · 0 comments
Open

Using mocks #3

MikeWarren2014 opened this issue Mar 7, 2018 · 0 comments

Comments

@MikeWarren2014
Copy link

A central part of unit testing, is the creating of mocks and stubs. My particular use case of this :
I wish to test the following code:

/* Helps map the columns of the spreadsheet to the correct arguments in a constructor
 * Parameters:
 *	• arguments : (String[]) the string representation of the arguments of a constructor
 * NOTE: be careful not to pass the constructor an Array, but to pass it a list of arguments. This class contains an argIndices array that specifies the index 
 *	of the spreadsheet column to use for this.argNames[j]
 * use this.argIndices to refer to get the index of the columns that the property identified by the jth element of this.argNames is supposed to point to
 */
function DataStoreHelper() {
  this.argNames = arguments;	// TODO: check type of arguments
  this.argIndices = new Array(this.argNames.length);
  /* takes an array of column names and assigns the indices where they can be found, to this.argIndices
   */
  this.sortColumns = function (columnNames) { 
    // for each argument name ...
    for (var j in this.argNames)
    {
    	var k = 0; 
      // ... sift thru the column names ...
      for (k in columnNames) {
          // ... to find the column name that contains the argument name ...
        if (columnNames[k].toLowerCase().indexOf(this.argNames[j]) !== -1) break;
        else if (k == columnNames.length - 1) k = -1;
        
      }
      // if we found it, we write it to this.argIndices
      if (k > -1) this.argIndices[j] = k;
      // otherwise, we have spreadsheet error.
      else throw new Error('Spreadsheet is missing column for constructor argument: ' + this.argNames[j]);
      Logger.log('this.argNames[%s] == %s\tthis.argIndices[%s] == %s\n',
        j, 
        this.argNames[j],
        j,
        this.argIndices[j])
    }
  }
}
// static const strings
DataStoreHelper.POTHOLE_NUMBER  = 'number';
DataStoreHelper.LATITUDE        = 'latitude';
DataStoreHelper.LONGITUDE       = 'longitude';
DataStoreHelper.ADDRESS         = 'address';
DataStoreHelper.SURFACE_AREA    = 'area';
DataStoreHelper.BAGS            = 'bags';
DataStoreHelper.SNAPPED_TO_ROAD = 'snappedtoroad';
DataStoreHelper.FILLED          = 'filled';
DataStoreHelper.IMAGE_URL       = 'imageurl';
// derived helper classes for PotholeData,PotholeDataNoCoords
function PotholeDataHelper() { 
  DataStoreHelper.call(this, 
                       DataStoreHelper.POTHOLE_NUMBER,
                       DataStoreHelper.LATITUDE,
                       DataStoreHelper.LONGITUDE,
                       DataStoreHelper.SNAPPED_TO_ROAD,
                       DataStoreHelper.FILLED,
                       DataStoreHelper.SURFACE_AREA,
                       DataStoreHelper.BAGS,
                       DataStoreHelper.IMAGE_URL);
}
PotholeDataHelper.prototype = Object.create(DataStoreHelper.prototype);

function PotholeDataNoCoordsHelper() { 
  DataStoreHelper.call(this,
                       DataStoreHelper.POTHOLE_NUMBER,
                       DataStoreHelper.ADDRESS,
                       DataStoreHelper.FILLED,
                       DataStoreHelper.SURFACE_AREA,
                       DataStoreHelper.BAGS,
                       DataStoreHelper.IMAGE_URL);
}
PotholeDataNoCoordsHelper.prototype = Object.create(DataStoreHelper.prototype);

I have scanned the API docs, for how to create mocks, but could find no functions in there to help. I looked at some example code, but also found zilch. Is there any way that we can test these business functions, which are supposed to map the argument indices of another business function with the respective columns of a spreadsheet, without having to either construct the helper objects for real or rolling my own mock logic?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant