-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sinon not stubbing a function in feature test #40
Comments
Ok, so. let computer = new Computer
await sinon.stub(computer, 'choice').returns("rock") no2 sinon.stub(Computer, 'choice').get(function getterFn() {
return 'rock';
}); no3
The first one works with the Computer set up like that: class Computer {
constructor(choice) {
this.choice = function () {
let num = Math.floor((Math.random() * 3) + 1)
if (num==1) {
return "rock"
} else if (num==2) {
return "paper"
} else {return "scissors"}
}
}
} The number 2 and 3 works with this: const Computer = {
choice: function () {
let num = Math.floor((Math.random() * 3) + 1)
if (num == 1) {
return "rock"
} else if (num == 2) {
return "paper"
} else { return "scissors" }
}
} I have tested all the three versions (with unit tests, because TDD) and they do their job, they return the value, for example this comes back green: it("stub1 works", () => {
let comp = new Computer
sinon.stub(comp, 'choice').returns("rock")
expect(comp.choice()).to.eql("rock")
}); The problems start when I try to insert it in the feature test. It just wouldnt do it. I suspect it happens because tests in the feature tests are async? Right? Wrong? What do? |
Eh, i dont know what happened, but apparently i closed the issue somehow by mistake. |
Impressive progress. I would need to see HOW you use it in your acceptance tests. Please post an example. |
Generally, if you use
Now every method that uses this will return I would just throw this in into the before-block of my test and see what happens... |
Heres my feature test: require('../spec.helper');
var sinon = require('sinon');
context('The game', () => {
before(async () => {
await browser.init()
await browser.visitPage('http://localhost:8080/')
});
beforeEach(async () => {
await browser.page.reload();
sinon.stub(Math, 'floor').returns(1);
});
after(() => {
browser.close();
});
it("returns 'tie' message if user picks 'rock' and computer picks 'rock'", async () => {
await browser.clickOnButton("button[id='rock']");
let content = await browser.getContent("div[id='message']");
expect(content).to.contain('You chose rock, computer chose rock. It is a tie.');
});
}); Tried this, but still get assertion errors. Tried all the three options - I mean, this is my whole thing here: document.addEventListener('DOMContentLoaded', () => {
let choice = document.querySelectorAll("button[tag='choice']")
for(var i = 0; i < choice.length; i++){
choice[i].addEventListener("click", function() {
event.preventDefault();
let computer= new Computer
let computersChoice = computer.choice();
if (userChoice == computersChoice) {
document.getElementById("message").innerHTML = `You chose ${userChoice}, computer chose ${computersChoice}. It is a tie.`
} else if (
(userChoice == "rock" && computersChoice == "scissors") ||
(userChoice == "scissors" && computersChoice == "paper") ||
(userChoice == "paper" && computersChoice == "rock")
) {
document.getElementById("message").innerHTML = `You chose ${userChoice}, computer chose ${computersChoice}. You win!`
} else {
document.getElementById("message").innerHTML = `You chose ${userChoice}, computer chose ${computersChoice}. You lose!`
}
})
}
}) Can the problem be that the whole computer choice / math function is inside this monstrous event listener block? If we look at the feature test, the flow goes like this:
Can it be that the step 2 overrides the stub? As far as i have understood from Sinon's docs it shouldnt be the case. I mean otherwise, how are we ever supposed to stub anything? (many questions) |
heres my repo: https://github.com/zanenkn/rock_paper_scissors |
Problem
Sinon is not stubbing my function
Background
Testing the RPS. Users choice is determined by listening for the button click. Computers choice is determined by method that picks a random between them three. Winner is determined by comparing these two. For test purposes I want to stub the computers choice - say that it is "rock" at all times. Sinon just wouldn't cooperate - tests are coming back sometimes red and sometimes green - the computers choice is still random.
Feature test
Implementation code
How did you try to solve the problem?
Read the documentation in Sinon js
Rewrote the Computer as so
and the stub as so
(didnt do much)
The text was updated successfully, but these errors were encountered: