I am currently building a Rock, Paper, Scissors game in Javascript and using TDD to guide my code. I am trying to run a Jasmine test that forces one of my functions to return a set value. I want my "compChoice" function to return a random element from the 'choices' array ["Rock", "Paper", "Scissors"] and in my test want to set this to 'Rock'. My tests are below.
describe("Game", function() {
var game;
beforeEach(function(){
game = new Game();
});
describe('user choice', function(){
it('should equal the choice the user selected', function(){
game.userSelect("Rock");
expect(game.userChoice).toEqual("Rock")
});
})
describe('draw', function(){
it('should equal true if user choice and comp choice are the same', function() {
game.userSelect("Rock");
spyOn(game,'compChoice').and.returnValue("Rock");
expect(game.opponentChoice).toEqual("Rock")
// expect(game.draw).toEqual(true);
});
})
});
I can tell there's something wrong with my spyOn as my test comes back with "Expected ' ' to equal 'Rock'."
I don't know why it's not calling the spy and setting the value to "Rock" like I asked.
My actual code is down below for reference:
function Game() {
this.choices = ["Rock","Paper","Scissors"];
this.userChoice = "";
this.opponentChoice = "";
}
Game.prototype.userSelect = function(choice){
this.userChoice = choice;
}
Game.prototype.compChoice = function(){
this.opponentChoice = this.choices[Math.floor(Math.random()*this.choices.length)];
return this.opponentChoice;
}