0

How would one write up a Jasmine Unit Test, based around a random outcome generator?

For example, how does one write a test around the result of a random dice throw (or colour picker, or weather setting if it were a string)?

The dice code itself is along the lines of:

Dice.prototype.rollDice = function() {
1 + Math.floor(Math.random() * 6);
};

And I am aware that what is written below is incorrrect/ incomplete, I have simply written it as an example of where I am stuck:

it("can generate a random number from the players throw", function() {
var dice = new Dice();
dice.rollDice();
expect(----------------------).toEqual(----------------);
});

I have little experience with Jasmine Unit testing, and would appreciate a demonstration of a complete working example

Many thanks

Mervodactyl
  • 101
  • 1
  • 1
  • 6
  • 2
    Possibly META, but I'd recommend skipping over testing this sort of thing. You can bet that the Math package has been tested to death and there is not much sense in checking to see that its results are in fact random. There's no way to do it anyway, considering that there's no way to say what you're going to end up rolling anyway. –  Jun 30 '15 at 18:40
  • I can only think of testing if the result is between 1 and 6 – Felipe Skinner Jun 30 '15 at 18:47
  • @FelipeSkinner Yes, but when you write an assertion in a test you need to be precise. You can certainly check to see if something is between 1 and 6, but it is not possible to check what value between one and six because you'll need to know in advance when the test is written. So the question asker will only be able to check a range of values, not specific values. –  Jun 30 '15 at 19:08
  • how so? cant i write `expect({rolled value}).toBeGreaterThan(0)` and `expect({rolled value}).toBeLessThan(7)` ? – Felipe Skinner Jun 30 '15 at 20:49
  • Possible duplicate of [Unit testing - how do I test a function that returns random output?](http://stackoverflow.com/questions/2618043/unit-testing-how-do-i-test-a-function-that-returns-random-output) – Raedwald Jan 23 '16 at 15:54

3 Answers3

0

You can google for "how to test randomness" but, as @radicalmatt said, you just do not test this kind of behavior in your application, or at least not as part of your regular test suite.

If I were to write code for a "dice" that is supposed to give a random number from 1 to 6, I would manually test it after writing the code, but I would not write an automated test. That is to say, I would write a little throwaway program that would execute rollDice() a million times and then output the relative frequency of each result. I would expect the results to include only the numbers 1 to 6, and I would expect all frequencies to be comfortably close to 16. If I'm satisfied with the outcome, I would consider it tested. I may add a comment like "Remember to test this code if you modify it".

abl
  • 5,970
  • 4
  • 25
  • 44
0

I would write it as two expectations:

expect({rolled value}).toBeGreaterThan(0);
expect({rolled value}).toBeLessThan(7);
johnmcase
  • 1,769
  • 2
  • 16
  • 27
  • This suggestion has worked, written as such: expect(dice.dieFace).toBeGreaterThan(0); expect(dice.dieFace).toBeLessThan(7); have you any suggestions for strings? - I'm sorry I don't have enough points to vote up your suggestion but thank you nonetheless – Mervodactyl Jun 30 '15 at 19:47
  • Try something like: expect(['these', 'are', 'the', 'valid', 'options']).toContain(actualValue); – johnmcase Jul 02 '15 at 17:36
0

just don't use math.random in your tests. mock it/refactor and test everything else. for example you should test if having any possible (a few edge cases) output from math.random, your rollDice function still produce valid results

piotrek
  • 13,982
  • 13
  • 79
  • 165