Page 1 of 1

unit testing again...

Posted: 20 Jan 2013, 00:03
by aubergine
So, I'm finally getting round to finishing the unit testing for my Util API, and I've run in to a stumbling block -- maybe someone with more experience of unit testing can help?

I've got a function like this, within the API's sandbox:

Code: Select all

	var difficultyIndex = Math.max( 0, ([EASY, MEDIUM, HARD, INSANE]).indexOf(difficulty) );

	_global.addConst(
		"difficultyScale",
		function(/* easy, medium, hard, insane */) {
			return arguments[ Math.min( arguments.length-1, difficultyIndex ) ];
		}
	);
That adds an immutable 'difficultyScale()' function to the global object (docs).

I can easily test different parameter combinations, but I can't test them in relation to different 'difficultyIndex' values.

Any ideas on how to accomplish that?

Re: unit testing again...

Posted: 21 Jan 2013, 19:37
by aubergine
Found a way to deal with above scenario -- I added an accessor to the _global.difficultyScale() method, which allows the difficultyIndex to be overriden like so:

Code: Select all

// set
difficultyScale.level = INSANE;
// get
var foo = difficultyScale.level; // INSANE