mardi 30 juin 2015

How to write up a unit test in Jasmine for a random outcome? (eg Dice Throw)

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

Aucun commentaire:

Enregistrer un commentaire