Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Unit Testing Improving Our Tests Answer: Writing Testable Code

my solution for testing random coordinates

hi, especially to Guil Hernandez & Joseph Fraley, and those taking this course,

i think i have come up with a test for the "getRandomCoordinates()" function you made modular in this step.

so the function, which i also made modularly exportable in JavaScript at the end of the player_methods.js file, i have written as thus —

...
function getRandomCoordinates() {
  var x = Math.floor(Math.random() * 9);
  var y = Math.floor(Math.random() * 9);
  return [x, y];
}
...

so in testing this in the player_test.js, i came up with the following testing each random array x, y coordinate pairing against each other, to make sure they were, indeed, all unique and random, using a two-level for-loop —

...
  describe('getRandomCoordinates', function () {
    var getRandomCoordinates = require('../game_logic/player_methods').getRandomCoordinates;
    var randCoordResult = {};

    it('each coordinate array in object should be different', function () {

      for(var i = 0; i < 10; i++) {
        randCoordResult[i] = getRandomCoordinates();
      }

      console.log(randCoordResult);

      for(var i = 0; i < 10; i++) {
        for(var j = 0; j < 10; j++) {
          if(i === j) {
            continue;
          } else {
            expect(randCoordResult[i]).to.not.eql(randCoordResult[j]);
          }
        }
      }
    });
  });
...

...and this test seems to work well each time, first printing out with console.log() the array and then testing they are, indeed, each unique and different against each other. however, one thing i am aware of, a truly random Math method will, by nature, occasionally pull up two sets of x, y coordinates in an array that are random, but identical. what to do to test for this is a bit beyond my knowledge. also, in the case of getRandomDirection(), because there are only two results of vertical or horizontal, that makes it much harder to detect randomness, because they will naturally be a number of duplicate results over time. but i thought i'd at least share this, what i found for testing random results.

best,

— faddah portland, oregon, u.s.a.

Oziel Perez
Oziel Perez
61,321 Points

One thing that was particularly good about Guil's approach was that he turned the original functions into "pure functions". This means that the function accepts coordinates as input and returns output rather than generating the coordinates within the function. This makes all the new functions easier to use and share with the rest of the application.